The first set of functions that we'll look at are those that deal
with strings. These functions let you determine a string's length,
search for a sub-string, and change the case of the characters
in the string, among other things.

Here are Perl's string
functions:

chomp(STRING) OR chomp(ARRAY) -- Uses the
value of the $/ special variable to remove endings from
STRING or each element of ARRAY. The line ending is only removed
if it matches the current value of $/.

chop(STRING) OR
chop(ARRAY)-- Removes the last character from a string or the last
character from every element in an array. The last character chopped is
returned.

chr(NUMBER) -- Returns the character represented by
NUMBER in the ASCII table. For instance, chr(65) returns the
letter A.

index(STRING, SUBSTRING, POSITION) -- Returns the position of the first
occurrence of SUBSTRING in STRING at or after POSITION.
If you don't specify POSITION, the search starts at the beginning
of STRING.

join(STRING, ARRAY) -- Returns a string that consists
of all of the elements of ARRAY joined together by STRING.
For instance, join(">>", ("AA", "BB", "cc")) returns
"AA>>BB>>cc".

lc(STRING) -- Returns a string with
every letter of STRING in lowercase. For instance, lc("ABCD")
returns "abcd".

lcfirst(STRING) -- Returns a string with the
first letter of STRING in lowercase. For instance,
lcfirst("ABCD") returns "aBCD".

length(STRING) -- Returns the
length of STRING.

rindex(STRING, SUBSTRING, POSITION) -- Returns
the position of the last occurrence of SUBSTRING in STRING at
or after POSITION. If you don't specify POSITION, the search
starts at the end of STRING.

split(PATTERN, STRING,
LIMIT) -- Breaks up a string based on some delimiter. In an array
context, it returns a list of the things that were found. In a scalar
context, it returns the number of things found.

substr(STRING, OFFSET, LENGTH) -- Returns a portion of
STRING as determined by the OFFSET and LENGTH parameters. If
LENGTH is not specified, then everything from OFFSET to the
end of STRING is returned. A negative OFFSET can be used to
start from the right side of STRING.

uc(STRING) -- Returns a
string with every letter of STRING in uppercase. For instance,
uc("abcd") returns "ABCD".

Ucfirst(STRING) -- Returns a
string with the first letter of STRING in uppercase. For instance,
ucfirst("abcd") returns "Abcd".

Note
As a general rule, if Perl sees a number where it expects a string, the
number is quietly converted to a string without your needing to do
anything.

Note Some of these functions use the special
variable $_ as the default string to work with. More information
about $_ can be found in Chapter on Files, and the Chapter
Special Variables.

The next few sections demonstrate some of these functions. After
seeing some of them work, you'll be able to use the rest of them.

Example: Changing a String's Value

Frequently, I find that I need to change part of a string's value,
usually somewhere in the middle of the string. When this need
arises, I turn to the substr()
function. Normally, the substr()
function returns a sub-string based on three parameters: the string
to use, the position to start at, and the length of the string
to return.

The substr() function starts
at the fifth position and returns the next three characters. The
returned string can be printed like in the above example, as an
array element, for string concatention, or any of a hundred other
options.

Things become more interesting when you put the substr()
function on the left-hand side of the assignment statement. Then,
you actually can assign a value to the string that substr()
returns.

If the third parameter-the length-is not supplied to substr(),
it simply returns the sub-string that starts at the position specified
by the second parameter and continues until the end of the string
specified by the first parameter.