chomp($myString);
$myString =~ s/\///g;
can i replace those two with
$myString =~ s/\s//g;
are there any difference? Please explain.
chomp($myString);
$myString =~ s/\///g;
can i replace those two with
$myString =~ s/\s//g;
are there any difference? Please explain.
Your first code will take a newline off the end of $myString if it exists and then remove all "/" characters. The second line of code will remove all whitespace characters. Is there a typo?
Maybe you want to know you can replace this:
with this:
If that's the question, then yes. Since a newline counts as whitespace, the second code example do the job of both lines above.
Chomp will get rid of newlines at the end of your string, but won't remove whitespace. A typical trim function uses the following two substitution lines:
The first line removes any spaces at the beginning of your string, and the second removes spaces after the end of the string.
From perldoc chomp:
chomp remove the newline from the end of an input record when you're worried that the final record may be missing its newline.
When in paragraph mode
($/ = "" )
, it removes all trailing newlines from the string. When in slurp mode ($/ = undef
) or fixed-length record mode ($/
is a reference to an integer or the like, see perlvar) chomp() won't remove anything.you can remove leading and trailing whitespace from strings like,