Detailed Explanation of Common Methods of Javascript String

  • 2021-07-04 18:03:58
  • OfStack

String

A string is one or more characters arranged from one, placed in single quotation marks or double quotation marks.

'abc'
"abc"

length Properties

The string in js is similar to an array, which is composed of 1 character pieced together in 1, so the length of the string can be obtained by using length attribute

var str = "hello"
str.length; // 5

Some methods commonly used in string

1. charAt()


str.charAt(n)

= > Returns the n th character of a string, or an empty string if it is not between 0 and str. length-1.


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''

2. indexOf()


indexOf(substr[,start])

= > Returns the first occurrence of substr in the string str, starting with the start position, and returns-1 if it does not exist.

start can be any integer with a default value of 0. If start < 0 to find the entire string (as if 0 had been passed in). If start > = str. length, the method returns-1 unless the string being looked for is an empty string, in which case str. length is returned.


var str = "javascript";
str.indexOf('s'); // 1
str.indexOf('s',6); // -1
str.indexOf('',11); // 10
str.indexOf('',8); // 8

3. lastIndexOf()


lastIndexOf(substr[,start])

= > Returns the last occurrence of substr in the string str, looking forward from the start position, and returns-1 if it does not exist.


'lastindex'.lastIndexOf('a'); // 1

4. substring()


str.substring(start[, end])

= > Returns characters from start to end (excluding), start and end are non-negative integers. If the end parameter (end) is omitted, it means truncation from position 1 of start to the end.


var str = 'abcdefg';
str.substring(1, 4); //"bcd"
str.substring(1); // "bcdefg"
str.substring(-1); //"abcdefg"  When a negative value is passed in, it is regarded as 0

5. slice()


str.slice(start[,end])

= > Returns characters from start to end (excluding), and can pass negative values


var str = 'this is awesome';
str.slice(4, -1); //" is awesom"

6. substr()


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
0

= > Returns a substring from the specified position to the specified length in str, which can be negative


var str = "Just give me a reason";
str.substr(5, 10); // "give me a "
str.substr(-4, 2); // "as"

7. replace()


str.replace(regexp|substr, newSubStr|function)

= > Substring to replace str


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
3

8. search()


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
4

= > Find whether str matches a regular expression. If the match is successful, the index of the first match of the regular expression in the string is returned; Otherwise, return-1. If a parameter passes in an irregular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
5

9. match()


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
6

= > Returns an array containing matching results, or null if there is no match. If a parameter passes in an irregular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)


var str = 'Javascript java';
str.match(/Java/); // ["Java"]
str.match(/Java/gi); // ["java", "Java"]
str.match(/ab/g); // null

10. split()


str.split([separator][, limit])

= > Returns an array, and the delimiter separator can be a string or a regular expression


var str = "javascript";
str.charAt(2); //'v'
str.charAt(12); //''
9

11. trim()


str.trim()

= > Remove the white space characters at the beginning and end of str and return a copy of str without affecting the value of the string itself


var str = ' abc ';
str.trim(); // 'abc'
console.log(str); // ' abc '

12. toLowerCase()


str.toLowerCase()

= > Converts str to lowercase and returns 1 copy of str without affecting the value of the string itself


var str = 'JavaScript';
str.toLowerCase(); // 'javascript'
console.log(str); // 'JavaScript'

13. toUpperCase()


str.toUpperCase()

= > Converts str to uppercase and returns 1 copy of str without affecting the value of the string itself


var str = 'JavaScript';
str.toUpperCase(); // 'JAVASCRIPT'
console.log(str); // 'JavaScript'

Related articles: