JS string method

All string methods will return a new string instead of changing the original string.

Find string

indexOf(src,index)
traverse the target string from the beginning to find out the index (position) of the first occurrence of matching src, return -1 if not found

lastIndexOf(src,index)
traverses the target string from the end, finds the index (position) of the first occurrence that matches src, returns -1 if it is not found

search(src) is the
same as indexOf, except that there is no second starting position parameter.
String extraction

slice(start, end)
extracts the string content from start to end. If end is greater than the total length of the string or is omitted, all strings after start are extracted. If a parameter is negative (negative values ​​are not suitable for IE8 And earlier versions), count from the end of the string.

var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7); //Banana

substring(start, end) is
similar to slice(), but does not accept input parameters with negative values

substr(start, length) is
similar to slice. The difference is that the second parameter is the length of the extracted part. If omitted, it will be extracted directly to the end of the string. Start supports negative.

charAt(index)
returns the string with the specified subscript (position) in the string, which is safer than using str[index] directly to get the character. If it is not found, it will directly return an empty string, but str[index] It will return undefined. At the same time, using str[index] just reads the string, and cannot change the value of the index position by str[index] ='A'.

charCodeAt()
returns the unicode encoding of the specified index character in the
string ; string segmentation

split(str)
divides the string into arrays according to str, omitting the separator, the returned array will contain the entire string in index [0]. If the delimiter is "", the returned array will be an array with a single character interval

var str = "Hello";
var arr = str.split(""); ['H','e','l','l','o']
var str = "a,b,c,d,e,f";
var arr = str.split(); // arr[0]为"a,b,c,d,e,f"

String splicing

concat(str1, str2, str3…)
concatenate multiple strings, you can also use the + operator to achieve

Other functions

replace(target, src)
replaces the target string in the string with the src string, only replaces the first match and is not case sensitive, but the first parameter supports regular expressions, that is, if you want to achieve case Insensitive replacement or replacement of all matching items can be solved by regularization.

str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");
var n = str.replace(/Microsoft/g, "W3School");

toUpperCase() convert
string to uppercase
toLowerCase() convert
string to lowercase
trim()
removes the blank characters on both sides, IE8 and below versions do not support this method. Using replace combined with regular can also be achieved, but it is relatively troublesome.

Summary reference: https://www.w3school.com.cn/js/js_string_methods.asp

Guess you like

Origin blog.csdn.net/dypnlw/article/details/114106690