Mega Code Archive

 
Categories / Perl / String
 

String Operations

Example                                  Meaning $str1 . $str2                            Concatenate strings $str1 and $str2 $str1 x $num                             Repeat $str1, $num times substr($str1, $offset, $len)             Substring of $str1 at $offset for $len bytes index($str1, $str2)                      Byte offset of string $str2 in string $str1 length(EXPR)                             Returns the length in characters of expression, EXPR rindex($str, $substr, POSITION)          Returns the position of the last occurrence of $substr in $str.                                          If POSITION is specified, start looking there.                                          If POSITION is not specified, start at the end of the string. chr(NUMBER)                              Returns character for ASCII number index. lc($str)                                 Returns a lowercase string uc($str)                                 Returns an uppercase string     #!/usr/bin/perl $x="A"; $y="B"; $z="*"; print $z x 10, "\n";              # Print 10 stars print $x . $y, "\n";              # Concatenate " print $z x 10, "\n";              # Print 10 stars print (($x . $y ."  ")  x  5 );   # Concatenate and print 5 times print "\n"; print uc($x . $y), "!\n";         # Convert string to uppercase $booleanResult = ("The" eq "the"); print '"The" eq "the"; Results in '; print " booleanResult = $booleanResult\n"; $booleanResult = ("the" eq "the") ; print '"the" eq "the"; Results in '; print " booleanResult = $booleanResult\n"; $booleanResult = ("The" ne "the"); print '"The" ne "the"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("the" ne "the"); print '"the" ne "the"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("a" lt "A"); print '"a" lt "A"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("A" lt "a"); print '"A" lt "a"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("a" le "a"); print '"a" le "a"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("aa" le "a"); print '"aa" le "a"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("a" gt "A"); print '"a" gt "A"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("a" gt "AA"); print '"a" gt "AA"; Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("The" cmp "the"); print '"The" cmp "the" Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("the" cmp "The"); print '"the" cmp "The" Results in '; print "booleanResult = $booleanResult\n"; $booleanResult = ("the" cmp "the"); print '"the" cmp "the" Results in '; print "booleanResult = $booleanResult\n"; $a = "The" x 3; print '$a = "The" x 3 ;Results in '; print "a = $a \n"; $word1 = "The "; $word2 = "beginning"; $a = $word1 . $word2; print '$a = $word1 . $word2 ; Results in '; print "a = $a \n";     Operator     Means                  Numeric Equivalent .            Concatenate            + x            Multiply (replicate)   * eq           Equal to               = ne           Not equal to           != lt           Less than              < gt           Greater than           > le           Less than or equal to  <= cmp          Compare                <=>