Javascript - String Manipulation Methods

Introduction

concat() concatenates one or more strings to another, returning the concatenated string as the result.

var stringValue = "hello ";
var result = stringValue.concat("world");
console.log(result);            //"hello world"
console.log(stringValue);       //"hello"

The value of stringValue remains unchanged.

The concat() method accepts any number of arguments, so it can create a string from any number of other strings.

var stringValue = "hello ";
var result = stringValue.concat("world", "!");

console.log(result);            //"hello world!"
console.log(stringValue);       //"hello"

The addition operator (+) is used more often when concatenating strings.

To get a substring use slice(), substr(), and substring().

All three methods return a substring, and all accept either one or two arguments.

The first argument is the position where capture of the substring begins; the second argument, if used, indicates where the operation should stop.

For slice() and substring(), this second argument is the position before which capture is stopped: all characters up to this point are included except the character at that point.

For substr(), the second argument is the number of characters to return.

If the second argument is omitted in any case, it is assumed that the ending position is the length of the string.

slice(), substr(), and substring() do not alter the value of the string itself, leaving the original unchanged.

var stringValue = "hello world";
console.log(stringValue.slice(3));        //"lo world"
console.log(stringValue.substring(3));    //"lo world"
console.log(stringValue.substr(3));       //"lo world"
console.log(stringValue.slice(3, 7));     //"lo w"
console.log(stringValue.substring(3,7));  //"lo w"
console.log(stringValue.substr(3, 7));    //"lo worl"

For the slice() method, a negative argument is treated as the length of the string plus the negative argument.

For the substr() method, a negative first argument is treated as the length of the string plus the number, whereas a negative second number is converted to 0.

For the substring() method, all negative numbers are converted to 0.

var stringValue = "hello world";
console.log(stringValue.slice(-3));         //"rld"
console.log(stringValue.substring(-3));     //"hello world"
console.log(stringValue.substr(-3));        //"rld"
console.log(stringValue.slice(3, -4));      //"lo w"
console.log(stringValue.substring(3, -4));  //"hel"
console.log(stringValue.substr(3, -4));     //"" (empty string)

String trim() Method

The trim() method creates a copy of the string, removes all leading and trailing white space, and then returns the result.

var stringValue = "    hello world    ";
var trimmedStringValue = stringValue.trim();
console.log(stringValue);            //"    hello world    "
console.log(trimmedStringValue);     //"hello world"

trim() returns a copy of a string, the original string remains intact with leading and trailing white space in place.

Related Topics