in reply to Stripping characters from strings
use strict; #Always! my $string = "This is a test string. 123\n"; chomp($string); # removes the "\n" from the end of $string. chomp($string); # no-op. chomp only removes white-space: \t, ' ', \n, +etc... chop($string); # removes the '3' from the end of $string. chop($string); # removes the '2'... get the idea? print $string, $/; # print the string, and a new-line. $string =~ s/a //; # replaces an appearance of 'a ' with '' print $string, $/; # print the string, and a new-line. # should read: "This is test string. 1" $string =~ tr/.//d; # remove all periods. print $string, $/; $string =~ s/.$//; # removes the last char. slower then chop. $string =~ s/..$//; # removes the last two characters. my $n = 6; $string =~ s/.{$n}$//; # removes the last $n characters. print $string, $/;
|
---|