String Operations : String « String « Perl






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                <=>

   
    
    
    
    
  








Related examples in the same category

1.String Operators: the right and wrong ways to perform an equivalence check on a string:
2.String plus
3.String variable
4.Strings are normally delimited by a matched pair of either double or single quotes.
5.ASCII Character Codes
6.A string is a sequence of bytes (characters) enclosed in quotes.
7.Calculation on string
8.Use print to output string
9.Append two string value
10.Perl 5 String Functions
11.Using ~ operator to check if a scalar is a string type variable
12.String Literals
13.String operators
14.String value reference