15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Manipulating Strings 167<br />

Most <strong>programming</strong> languages use either the plus sign (+) or the ampersand<br />

(&) symbol as the concatenation operator, such as<br />

Name = “Joe “ + “Smith”<br />

or<br />

Name = “Joe “ & “Smith”<br />

In the Perl language, the concatenation symbol is the dot (.) character,<br />

such as<br />

$Name = “Joe “ . “Smith”;<br />

In the preceding examples, the concatenation operator takes the string<br />

“Joe “ and combines it with the second string “Smith” to create a single<br />

string that contains “Joe Smith”.<br />

When concatenating strings, you may need to insert a space between the<br />

two strings. Otherwise, the concatenation operator smashes both strings<br />

together like “JoeSmith”, which you may not want.<br />

Book II<br />

Chapter 3<br />

Manipulating Data<br />

For more flexibility in manipulating strings, many <strong>programming</strong> languages<br />

include built-in string functions. These functions can help you manipulate<br />

strings in different ways, such as counting the number of characters in a<br />

string or removing characters from a string. Table 3-5 lists some common<br />

built-in string functions found in many <strong>programming</strong> languages.<br />

Not all <strong>programming</strong> languages include these string functions, and if they do,<br />

they’ll likely use different names <strong>for</strong> the same functions. For example, Visual<br />

Basic has a Trim function <strong>for</strong> removing characters from a string, but Perl<br />

uses a substr function that per<strong>for</strong>ms the same task.<br />

Table 3-5<br />

Common Built-In String Functions<br />

String Function What It Does Example<br />

length (x) Counts the number of characters in a length (Hi there!) = 9<br />

string (x), including spaces<br />

trim (x, y) Removes characters from a string trim (Mary, 1) = ary<br />

index (x, y) Returns the position of a string within index (korat, ra) = 3<br />

another string<br />

compare (x, y) Compares two strings to see if they’re compare (A, a) = False<br />

identical<br />

replace (x, y, z) Replaces one string from within another replace (Batter, att, ik) =<br />

Biker

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!