Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Remove newline character from start of string

by danj35 (Sexton)
on May 10, 2010 at 12:15 UTC ( [id://839212]=perlquestion: print w/replies, xml ) Need Help??

danj35 has asked for the wisdom of the Perl Monks concerning the following question:

Hopefully a nice and easy problem for the monks to solve here:

I have a variable ($string) that contains some script. The script in $string is more than one line long and the content can vary. At the start and the end of the variable are newline characters and I need to remove these. At the moment I've successfully been able to remove the newline character from the end of the string using chomp as so:

chomp $string;
Does anyone know how to remove the newline character from the start of $string? My first thought is to use regular expressions, although I wouldn't know how to write this, especially when there are likely to be other newline characters between the start and the end.

Many thanks :)

Replies are listed 'Best First'.
Re: Remove newline character from start of string
by moritz (Cardinal) on May 10, 2010 at 12:28 UTC
    Note that chomp depends on the current value of $/. I assume here that you want to emulate that behaviour.

    As a regex:

    $str =~ s{^\Q$/\E}{};

    With substr:

    if (substr($str, 0, length($/)) eq $/) { substr($str, 0, length($/), ''; }
    Perl 6 - links to (nearly) everything that is Perl 6.
      Or somewhat oddly,
      chomp(substr($str, 0, length($/));

      Caution: Contents may have been coded under pressure.
Re: Remove newline character from start of string
by Old_Gray_Bear (Bishop) on May 10, 2010 at 12:42 UTC
    Yet another way to do it:
    chomp($string); my $rev = reverse($string); chomp($rev); $string = reverse($rev);
    Note: Coded Before Coffee; no warranties, no promises.

    ----
    I Go Back to Sleep, Now.

    OGB

      I like this. Nice and simple. Although thanks to everyone else for providing alternative solutions that also work. Monks rule once more :)

      Nice, but that works only if $/ eq reverse($/). Otherwise, you also need local $/=reverse $/ before chomp($rev).

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Remove newline character from start of string
by Ratazong (Monsignor) on May 10, 2010 at 12:20 UTC
    Try this:
    my $str = "\nxyz\nabc\n"; $str =~ s/^\n//;
    HTH, Rata
Re: Remove newline character from start of string
by Marshall (Canon) on May 10, 2010 at 13:05 UTC
    To remove all new lines within $line:
    $line =~ tr/\n//d;

    $line =~ s/^\n//; #remove leading one
    $line =~ s/\n$//; #remove last one or better use chomp($line)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found