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"
/* www . java 2s .c o m*/
stringValue = "hello ";
result = stringValue.concat("world", "!");
console.log(result); //"hello world!"
console.log(stringValue); //"hello"
The addition operator (+) is used more often than the concat() method.
The code above generates the following result.
slice(), substr(), and substring() all three methods return a substring of the string. They do not alter the value of the string itself.
They all accept either one or two arguments.
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"
/*from w ww . j a v a2 s . c o m*/
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)
The code above generates the following result.
The trim() method creates a copy of the string, removes all leading and trailing white space.
var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
console.log(stringValue); //" hello world "
console.log(trimmedStringValue); //"hello world"
The code above generates the following result.
Four methods perform case conversion: toLowerCase(), toLocaleLowerCase(), toUpperCase(), and toLocaleUpperCase().
The toLocaleLowerCase() and toLocaleUpperCase() methods are intended to be implemented based on a particular locale.
var stringValue = "hello world";
console.log(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
console.log(stringValue.toUpperCase()); //"HELLO WORLD"
console.log(stringValue.toLocaleLowerCase()); //"hello world"
console.log(stringValue.toLowerCase()); //"hello world"
The code above generates the following result.