04.11.2015 Views

javascript

Create successful ePaper yourself

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

String - Manipulation Methods<br />

Chapter 5: Reference Types<br />

Several methods manipulate the values of strings. The first of these methods is concat() , which is used<br />

to concatenate one or more strings to another, returning the concatenated string as the result. Consider<br />

the following example:<br />

var stringValue = “hello “;<br />

var result = stringValue.concat(“world”);<br />

alert(result);<br />

//”hello world”<br />

alert(stringValue); //”hello”<br />

The result of calling the concat() method on stringValue in this example is “ hello world “ — the<br />

value of stringValue remains unchanged. The concat() method accepts any number of arguments, so<br />

it can create a string from any number of other strings, as shown here:<br />

var stringValue = “hello “;<br />

var result = stringValue.concat(“world”, “!”);<br />

alert(result);<br />

alert(stringValue);<br />

//”hello world!”<br />

//”hello”<br />

This modified example concatenates “ world ” and “ ! ” to the end of “ hello “ . Although the concat()<br />

method is provided for string concatenation, the addition operator ( + ) is used more often and, in most<br />

cases, actually performs better than the concat() method even when concatenating multiple strings.<br />

ECMAScript provides three methods for creating string values from a substring: slice() , substr() ,<br />

and substring() . All three methods return a substring of the string they act on, and all accept either<br />

one or two arguments. The first argument is the position where capture of the substring begins; the<br />

second argument, if used, indicates where the operation should stop. For slice() and substring() ,<br />

this second argument is the position before which capture is stopped (all characters up to this point are<br />

included except the character at that point). For substr() , the second argument is the number of<br />

characters to return. If the second argument is omitted in any case, it is assumed that the ending position<br />

is the length of the string. Just as with the concat() method, slice() , substr() , and substring() do<br />

not alter the value of the string itself — they simply return a primitive string value as the result, leaving<br />

the original unchanged. Consider this example:<br />

var stringValue = “hello world”;<br />

alert(stringValue.slice(3)); //”lo world”<br />

alert(stringValue.substring(3)); //”lo world”<br />

alert(stringValue.substr(3)); //”lo world”<br />

alert(stringValue.slice(3, 7)); //”lo w”<br />

alert(stringValue.substring(3,7)); //”lo w”<br />

alert(stringValue.substr(3, 7)); //”lo worl”<br />

In this example, slice() , substr() , and substring() are used in the same manner, and in most cases<br />

return the same value. When given just one argument, 3 , all three methods return “ lo world ” , because<br />

the second “ l ” in “ hello ” is in position 3. When given two arguments, 3 and 7 , slice() and<br />

substring() return “ lo w ” (the “ o ” in “ world ” is in position 7, so it is not included), while substr()<br />

returns “ lo worl ” because the second argument specifies the number of characters to return.<br />

There are different behaviors for these methods when an argument is a negative number. For the<br />

slice() method, a negative argument is treated as the length of the string plus the negative argument.<br />

135

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

Saved successfully!

Ooh no, something went wrong!