Perl Programming/Strings

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Basic Variables Index Next: Numbers


Contents

[edit] Strings

[edit] Introduction to Strings

A string is a sequence of characters, like "Hello World!". Strings can be expressed by surrounding a group of characters with double (") or single (') quotes, though the two methods are interpreted differently. Here are some example strings

"Hello Billy!"  #This is a string
'Hello Billy!'  #This is also a string
"Hello Mother!' #WRONG - this is not a string because it is not encapsulated by either two double quotes
or two single quotes.

[edit] Double Quoted Strings

When you use a double quote (") to represent a string, you are saying that you want some certain values in a string to be changed to other values. This may sound a little confusing, but this example should help you understand:

"Hello World!\n" 

Notice that if you printed this string in a program, the output would not be "Hello World!\n", it would be Hello World! followed by a newline. This is because when you are using double quotes, the \n character is interpreted as what is called a backslash escape. Some common backslash escapes are:

  • \n - Newline
  • \t - Tab
  • \b - Backspace
  • \L - Lowercase Next Letter
  • \U - Uppercase Next Letter
  • \a - Ring System Bell

These are just some of the escapes that you can use in double quoted strings. When you learn about variables and math operations in the next couple of chapters, you will see that the values of variables can also be automatically inserted into strings. For now, however, let's move on to single quoted strings.

[edit] Single Quoted Strings

Single quoted strings are represented by using ' to enclose the string. Unlike a double quoted string, a single quoted string expresses exactly what you type in. In single quoted strings, you cannot use the backslash escapes, and values of variables will not be automatically substituted. Here's an example:

#!/usr/bin/perl

use warnings;

print 'Hello World!\n';

This little program is very much like your first program except for the quotes used in the string to be printed. This makes a huge difference. Instead of printing "Hello World!" and a newline, this modified version will print "Hello World!\n". As you can see, what you type is interpreted literally.

If you need to use a single quote inside a string, you can do so by using \'. For example:

 print 'Those are Mark\'s keys';

[edit] String Operators

Operators manipulate two or more strings in some way.

[edit] The Concatenation Operator

Perl uses the . operator to concatenate or connect two strings together, like this:

"Hello" . "World"  # This is the same as "HelloWorld"

If you want to make the string have a space between Hello and World you could write it like this:

"Hello" . " " . "World"  # This is the same as "Hello World"

Or like this:

"Hello" . " World" # This is the same as "Hello World"

[edit] The x Operator

This is called the string repetition operator and is used to repeat a string. All you have to do is put a string on the left side of the x and a number on the right side. Like this:

"Hello" x 5 # This is the same as "HelloHelloHelloHelloHello"

If you wish to insert a line break after each output of the string, use:

"Hello\n" x 5

[edit] Exercises

  • Write a program that uses the . operator to print "Hello Sir!".
  • Write another program which uses the x operator to print "HelloHelloHelloHello". Put comments in this program that explain how it works
  • Remember to take some time to play with single and double quoted strings, the more practice you get, the better you will be.
Previous: Basic Variables Index Next: Numbers
Personal tools