Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

add a character at the end of a string

by rsennat (Beadle)
on Dec 13, 2005 at 11:23 UTC ( #516285=perlquestion: print w/ replies, xml ) Need Help??
rsennat has asked for the wisdom of the Perl Monks concerning the following question:

hi all,

i need to add a character "/" at the end of the string, only if its missing.
my $string = "/tmp/check"; # append / at the end my $syring11 = "/tmp/check/"; # dont append /
thanks
rsennat

Comment on add a character at the end of a string
Download Code
Re: add a character at the end of a string
by reasonablekeith (Deacon) on Dec 13, 2005 at 11:27 UTC
    I always do it like this...
    $string =~ s|/?$|/|;

    although there are countless other ways. This one works by matching a trailing slash (if there is one) and 'replacing' it with a new trailing slash.

    an alternative would be as follows..

    $string .= '/' if $string !~ m|/$|;
    ---
    my name's not Keith, and I'm not reasonable.
      If I am right, /?$ means, the last slash in the string is replaced with /.

      can you get me any links or nodes here to read more on perl substitution operator.

      Thanks
      rsennat
        ok, here goes...

        the / means just that, match a forward slash
        the ? then says that you don't have to match the preceding character, but it will try to if it's there
        Finally the $ ties the match to the very end of the string

        So, if your string ends with a / it will be matched, and replaced with another slash, with no net effect.

        However, if your script doesn't end with a slash, the whole substitution still matches because the slash was optional. In this case all we're matching is the 'end' of the string. The substitution then replaces this (zero-width) match with the new slash, which has the net effect in inserting the trailing slash.

        for futher reading I’d recommend ‘perldoc perlre’ and the excellent “Mastering Regular Expressions” published by O’Reilly (which is superb)

        ---
        my name's not Keith, and I'm not reasonable.
Re: add a character at the end of a string
by kulls (Hermit) on Dec 13, 2005 at 11:32 UTC
    I tried little bit here,
    $string.="/" if(not $string =~/\/$/ );

    kulls
Re: add a character at the end of a string
by Tomte (Priest) on Dec 13, 2005 at 11:40 UTC

    I guess there are alternatives, but it works:

    # can't be called with a literal string as arg # but that wasn't asked for :) sub appendMissingSlash { return $_[0] .= substr($_[0], -1) eq "/" ? "" : "/"; } my $string = "/tmp/check"; appendMissingSlash($string); print $string,"\n"; __END__ /tmp/check/

    Update: I didn't use a regex, as other examples given suggest, because we know the position and the character to check for, using the regex engine seemed overkill to me

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      I didn't use a regex, as other examples given suggest, because we know the position and the character to check for, using the regex engine seemed overkill to me

      I this case, it's a fixed length string, at the end of the string, so it's a rather efficient match with regex -- if you were really concerned with speed, you'd have not wanted to append an empty string when you didn't have to:

      In my opinion, it's a case of premature optimization, as with the differences so insignificant for the top three, it's not worth worrying about. (multiple runs don't have a consistent leader, and variations in versions of perl might cause different results) ... and odds are, this one replacement is a very small part of the overall program, so I wouldn't even be opposed to the slowest one, as it's the shortest to type.

        In my opinion, it's a case of premature optimization,
        In mine, it's a case of using the right tool for the job, not contradicted (again IMO) by the benchmark results :)

        regards,
        tomte


        An intellectual is someone whose mind watches itself.
        -- Albert Camus

Re: add a character at the end of a string
by ikegami (Pope) on Dec 13, 2005 at 15:30 UTC

    The portable way would be to use core module File::Spec's catpath to join the dir to the filename instead of playing with slashes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://516285]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-07-30 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls