Common Javascript string Methods
String
A string is one or more characters arranged together and placed in single or double quotation marks.
'Abc'
"Abc"
Length attribute
The strings in js are similar to arrays and are composed of one character. Therefore, you can use the length attribute to obtain the length of a string.
Var str = "hello"
Str. length; // 5
Common Methods of strings
1. charAt ()
str.charAt(n)
=> Returns the nth character of the string, if not 0 ~ Returns an empty string between str. length-1.
var str = "javascript";str.charAt(2); //'v'str.charAt(12); //''
2. indexOf ()
indexOf(substr[,start])
=> Returns the position where substr appears for the first time in the string 'str'. It starts from the 'start' position and returns-1 if it does not exist.
Start can be any integer and the default value is 0. If start is less than 0, the entire string is searched (as if 0 is input ). If start> = str. length,-1 is returned for this method. Unless the searched string is an empty string, str. length is returned.
var str = "javascript";str.indexOf('s'); // 1str.indexOf('s',6); // -1str.indexOf('',11); // 10str.indexOf('',8); // 8
3. lastIndexOf ()
lastIndexOf(substr[,start])
=> Return the position where substr is located at the end of the str string. start from the start position and start searching. If it does not exist,-1 is returned.
'lastindex'.lastIndexOf('a'); // 1
4. substring ()
str.substring(start[, end])
=> Returns the characters from start to end (not included). start and end are non-negative integers. If the end parameter is omitted, the end parameter is truncated from the start position until the end.
Var str = 'abcdefg'; str. substring (1, 4); // "bcd" str. substring (1); // "bcdefg" str. substring (-1); // "abcdefg" is regarded as 0 when a negative value is input.
5. slice ()
str.slice(start[,end])
=> Return the characters from start to end (not included). Negative values can be passed.
var str = 'this is awesome';str.slice(4, -1); //" is awesom"
6. substr ()
str.slice(start[,end])
=> Return the substring from the specified position to the specified length in str. The start value can be a negative value.
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)
=> Replace the substring of str
var str = "do you love me";str.replace('love','hate'); // "do you hate me"
8. search ()
str.search(regexp)
=> Check whether str matches a regular expression. If the match succeeds, the index of the first matching item of the regular expression in the string is returned. Otherwise,-1 is returned. If a parameter is passed in as an non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj ).
var str = 'I love JavaScript!';str.search(/java/); // -1str.search(/Java/); // 7str.search(/java/i); // 7str.search('Java'); // 7
9. match ()
str.match(regexp)
=> Returns an array containing matching results. If no matching item exists, null is returned. If a parameter is passed in as an non-regular 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. The separator can be a string or regular expression.
var str = "Hello?World!";str.split(); // ["Hello?World!"]str.split(''); // ["H", "e", "l", "l", "o", "?", "W", "o", "r", "l", "d", "!"]str.split('?'); // ["Hello", "World!"]str.split('',5); // ["H", "e", "l", "l", "o"]
11. trim ()
str.trim()
=> Removes spaces at the beginning and end of str and returns a copy of str without affecting the value of the string.
var str = ' abc ';str.trim(); // 'abc'console.log(str); // ' abc '
12. toLowerCase ()
str.toLowerCase()
=> Converts str to lowercase and returns a copy of str without affecting the value of the string.
var str = 'JavaScript';str.toLowerCase(); // 'javascript'console.log(str); // 'JavaScript'
13. toUpperCase ()
str.toUpperCase()
=> Converts str to uppercase and returns a copy of str without affecting the value of the string.
var str = 'JavaScript';str.toUpperCase(); // 'JAVASCRIPT'console.log(str); // 'JavaScript'
The above is a detailed explanation of common Javascript string methods introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!