15 common JavaScript string manipulation methods

Keywords: Javascript encoding Attribute ECMAScript Firefox

1 Initialization

//Common Initialization Methods
var stringVal = "hello iFat3";
//Constructor Creation Method
var stringObj = new String("hello iFag3");

2 length attribute

var stringVal = "hello iFat3";
//Output 11
console.log(stringVal.length);

3 charAt()

Returns the character at a given position

var stringVal = "hello iFat3";
//Output e
console.log(stringVal.charAt(1));

4 charCodeAt()

Returns the character encoding for a given location

var stringVal = "hello iFat3";
//Output 101, e character encoding
console.log(stringVal.charCodeAt(1));

5 concat()

String stitching method, in most cases, using plus sign to stitch strings is easier.

var stringVal = "hello ";
//Can accept any number of parameters
var result = stringVal.concat("iFat3","!");
//Output hello 
console.log(stringVal);
//Output hello iFat3!
console.log(result);

6 substr()

String intercepts, accepting one or two parameters, the first specifying the start position, and the second specifying the number of characters returned.

var stringVal = "hello iFat3";
//Output lo iFat3
console.log(stringVal.substr(3));
//Output lo iF
console.log(stringVal.substr(3,5));

7 substring()

String intercepts, accepting one or two parameters, the first specifying the start position and the second specifying the end position.The slice() and substring() methods are basically the same in use on the operation string.

var stringVal = "hello iFat3";
//Output lo iFat3
console.log(stringVal.substr(3));
//Output lo
console.log(stringVal.substring(3,5));

8 indexOf() and lastIndexOf()

Substring search method, returns the location of the substring, returns -1 not found, or accepts the second parameter, indicating the start of the search.The lastIndexOf() method searches forward from the end of the string, and the second parameter of lastIndexOf() indicates the start of the search.

var stringVal = "hello iFat3 at";
//Output 8
console.log(stringVal.indexOf("at"));
//Output 12
console.log(stringVal.lastIndexOf("at"));
//Output 12
console.log(stringVal.indexOf("at",9));
//Output 8
console.log(stringVal.lastIndexOf("at",10));

9 trim()

ECMAScript 5 defines the trim() method for all strings.This method creates a copy of the string, removing all spaces in the prefix and suffix.Browsers that support this approach include IE9+, Firefox 3.5+, Safari 5+, Opera10.5+, and Chrome.

var stringVal = " hi iFat3 ";
//Output hi iFat3
console.log(stringVal.trim());

trim() implementation for incompatible browsers.

String.prototype.trim=function() {
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
var stringVal = " hi iFat3 ";
//Output hi iFat3
console.log(stringVal.trim());

10 toUpperCase() and toLowerCase()

String case conversion methods, toLocaleUpperCase() and toLocaleLowerCase(), are locale-specific implementations.

var stringVal = "hi iFat3";
//Output HI IFAT3
console.log(stringVal.toUpperCase());
//Output hi ifat3
console.log(stringVal.toLowerCase());

11 match()

String pattern matching method that returns a matched array.

var dus = "1du,2du,3du,4du";
var pattern = /.du/g;
var matches = dus.match(pattern);
//Output 4
console.log(matches.length);
//Output 1du
console.log(matches[0]);

12 search()

Returns the index of the first match in the string.

var dus = "1du,2du,3du,4du";
var pos = dus.search(/du/);
//Output 1
console.log(pos);

13 replace()

var dus = "1du,2du,3du,4du";
var result = dus.replace("du","fat");
//Output 1fat,2du,3du,4du
console.log(result);
result = dus.replace(/du/g,"fat");
//Output 1 fat, 2 fat, 3 fat, 4 fat
console.log(result);
result = dus.replace(/(.du)/g,"a($1)");
//Output a(1du),a(2du),a(3du),a(4du)
console.log(result);

14 split()

Splits a string into multiple substrings based on the specified splitter.

var dus = "1du,2du,3du,4du";
var result = dus.split(",");
//Output 4
console.log(result.length);

15 localeCompare()

Compare two strings and return 1, 0 or -1.

var val = "iFat3";
//Output 1
console.log(val.localeCompare("abc"));
//Output 0
console.log(val.localeCompare("iFat3"));
//Output-1
console.log(val.localeCompare("zoo"));

Posted by karnetics on Sat, 08 Jun 2019 11:20:42 -0700