As a full fledged programming language, Perl offers a wide range
of string handling functions. This differs from Unix shell scripting
or DOS batch processing, which offer limited string manipulation
capablities. The commands listed here are the most often used
string functions.
As a Quick & Dirty Guide (QDG), this document is not meant as
a tutorial, but as a reference.
The narrow width of the examples allows for easier export to mobile
computing platforms, such as Palm and 3G devices.
-------------------------------------------------------------------
$new = $one . $two
Concat (combine) strings.
$str2=$one.$two;
print"$one, $two, $str2 <br>\n";
ONE, TWO, ONETWO
-------------------------------------------------------------------
join(',',$last, $first)
Concat (join) strings.
$str2=join("-",$one,$two);
print"$one, $two, $str2 <br>\n";
ONE, TWO, ONE-TWO
-------------------------------------------------------------------
chomp($string)
Remove first character.
$str2=chomp($one);
print"$one $str2 <br>\n";
ONE 0
-------------------------------------------------------------------
chop($string)
Remove last character.
$str2=chop($one);
print"$one $str2 <br>\n";
ON E
-------------------------------------------------------------------
chr($value)
Output ASCII character.
$j=chr(65);
print"J: $j <br>\n";
J: A
-------------------------------------------------------------------
index($string, $substring, $start)
Return a character's position.
(First position is 0, not 1)
$j=index($two,"O",0);
print"In '$two', 'O' is $j <br>\n";
In 'TWO', 'O' is 2
-------------------------------------------------------------------
lc($string)
Change a string to lower case.
$str3=lc($one);
print"Was: $one Is: $str3 <br>\n";
Was: ON Is: on
-------------------------------------------------------------------
lcfirst($string)
Change first character's case.
$str2=lcfirst($two);
print"Was: $two Is: $str2 <br>\n";
Was: TWO Is: tWO
-------------------------------------------------------------------
length($string)
Number of characters in a string.
$j=length($two);
print"Length of $two is $j <br>\n";
Length of TWO is 3
-------------------------------------------------------------------
m/$string/
Match a substring.
if($two=~m/WO/){$j="match";}
print"$two, WO, $j <br>\n";
TWO, WO, match
-------------------------------------------------------------------
ord($string)
Return a string's ASCII value.
$j=ord("A");
print"J: $j <br>\n";
J: 65
-------------------------------------------------------------------
reverse($string)
Reverse the characters in a string.
$str2=reverse($two);
print"Was: $two Is: $str2 <br>\n";
Was: TWO Is: OWT
-------------------------------------------------------------------
s/x/y/g
Substitute character "x" for "y".
Use "g" for global substitution.
$str1=$ENV{'HTTP_USER_AGENT'};
print"$str1 <br>\n";
$srt1=~s/o/X/g;
print"$str1 <br>\n";
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
-------------------------------------------------------------------
split(/x/,$string)
Separate a string at character "x".
Example separates at spaces.
print"$str1 <br>\n";
($j,$k,$m)=split(/ /,$str1);
print"$j ($k) $m <br>\n";
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
CCBot/1.0 ((+http://www.commoncrawl.org/bot.html))
-------------------------------------------------------------------
substr($string,where,count)
Returns a portion of a string.
* where = start position from 0
* count = characters to retrieve
print"$str1, ";
$j=substr($str1,2,4);
print"$j <br>\n";
CCBot/1.0 (+http://www.commoncrawl.org/bot.html), Bot/
-------------------------------------------------------------------
uc($string)
Change string to upper case.
print"$str3, ";
$j=uc($str3);
print"$j <br>\n";
on, ON
-------------------------------------------------------------------
ucfirst($string)
Change case of first character.
print"$str3, ";
$j=ucfirst($str3);
print"$j <br>\n";
on, On
Done.