Getting Started
Introduction
Perl IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Top Sites
More Tutorials
Books
Magazines
Articles
NewsLetters
Mailing Lists
NewsGroups
Forums
User Groups
Talk Shows
Blogs
Clothing

GBIC >> Perl >> Information Center Tutorials >> Strings

Perl Information Center Tutorials - Strings
These tutorials were written to help you get a quick, but thorough, understanding of Perl - the scope of the language as well as it's specific capabilities.

Beginners Built-In Functions     Advanced CGI Applications

String Functions Summary
One of Perl's strengths is in the manipulation of strings, offering over 20 different built-in string functions. The functions can be categorized roughly into four areas:

    • Case-Conversion
     lc, lcfirst, uc, ucfirst
    • Last Character Removal
     chomp, chop
    • String Search/Extraction    
     index, rindex, split, substr 
    • Conversion/Formatting
     chr, ord, length, reverse,
     sprintf, tr///, y///
    

String Function Reference
Here's a quick reference of the available string functions, in alphabetical order. The functions are very simple to use.

Unless otherwise noted, these functions operate on $_ by default.

  • chomp - remove newline from end of string (if there is one)
        chomp $line;             # works on scalar, array or hash
        chomp ($line1, $line2);  # works on list
        

    Notes: Chomp is used to remove the newline character "\n", which Perl retrieves when it reads a line of text from a file. The character to be removed can be changed by changing the value of the $/ character.

  • chop - removes and returns last character of string
        chop $line;              # works on scalar, array or hash
        chop ($line1, $line2)    # works on lists
        
  • chr - get character with this ASCII value
        chr NUMBER              
        
  • index - first location of substring within a string
        index $string, $substring, $startpos   # does not use $_
        
    Note: $startpos is optional. -1 returned if not found.

  • lc - convert to lower case
        lc (expression)
        
  • lcfirst - convert only 1st letter to lower case
         lcfirst (expression)
        
  • length - number of characters
        length (expression)    # does not work on array or hash
        
  • ord - ASCII value of a character
         ord (expression)    # applies to 1st character in a string
        
  • reverse - reverse character order
        reverse LIST    # list context -elements in opposite order
                        # scalar context - concatenates also
                        # reverses characters in a string
                        # inverts a hash
        
  • rindex - right-to-left search for substring
        rindex $string, $substring, $startpos   # does not use $_
        
    Note: $startpos is optional. -1 returned if not found.

  • split - separate a string into substrings
        split /PATTERN/, (expression), LIMIT   
        Examples:
        ($one, $two, $three) = split ( /:/ "111:222:333" )
        @result = split ( / / "111 222 333" )
        
    Note: The expression default is $_ and can be a regex or simply an exact string.

  • sprintf - format a string
        sprintf FORMAT, LIST
    
        $result = sprintf("%08d", $number);      # up to 8 leading zeros
        $result = sprintf ("%.3f", $number);     # rounded
        
    See the bottom of the page for more information on FORMAT strings.

  • substr - return or change substring
        substr (expression), OFFSET, LENGTH, REPLACEMENT
        
    Note: Starts at OFFSET with LENGTH characters. Negative offset is from the right. Negative length means to trim string by that many characters.

  • tr/// - character substitution (same as y///)
        tr/SEARCHLIST/REPLACELIST   # regular expressions not supported
        $result =~ tr/A-Z/a-z/      # make $results characters lower case
        tr/A-Z/a-z                  # make $_ characters lower case
        tr/123/ABC/                 # replace 1 with A, 2 with B, 3 with C
        
  • uc - convert to upper case
        uc (expression)
        
  • ucfirst - convert only 1st letter to upper case
        ucfirst (expression)
        

sprintf Information
Perl's sprintf permits the following universally-known conversions:

     %%	a percent sign
     %c	a character with the given number
     %s	a string
     %d	a signed integer, in decimal
     %u	an unsigned integer, in decimal
     %o	an unsigned integer, in octal
     %x	an unsigned integer, in hexadecimal
     %e	a floating-point number, in scientific notation
     %f	a floating-point number, in fixed decimal notation
     %g	a floating-point number, in %e or %f notation

In addition, Perl permits the following widely-supported conversions:

     %X	like %x, but using upper-case letters
     %E	like %e, but using an upper-case "E"
     %G	like %g, but with an upper-case "E" (if applicable)
     %b	an unsigned integer, in binary
     %p	a pointer (outputs the Perl value's address in hexadecimal)
     %n	special: *stores* the number of characters output so far
          into the next variable in the parameter list

If you have suggestions or corrections, please let me know.