String
A string is one or more characters arranged together, placed in single or double quotes.
'abc'
"abc"
length attribute
The strings in js are similar to arrays, and are composed of characters one by one, so the length property can be used to obtain the length of the string.
var str = "hello"
str.length; // 5
Some common methods for strings
1. charAt()
str.charAt(n)
=> Returns the nth character of the string. If it is not between 0~str.length-1, it returns an empty string.
var str = "javascript";str.charAt(2); //'v'str.charAt(12); //''
2. indexOf()
indexOf(substr[,start])
=> Returns the position where substr first appears in the string str, starts looking at the start position, and if it does not exist, return -1.
start can be any integer, with the default value of 0. If start < 0, then search for the entire string (as if passed into 0). If start >= str.length, the method returns -1 unless the string being looked up is an empty string, then str.length is returned.
var str = "javascript";str.indexOf('s'); // 1str.indexOf('s',6); // -1str.indexOf('',11); // 10str.indexOf('',8); // 83. lastIndexOf()
lastIndexOf(substr[,start])
=> Returns the last position of substr in the string str, starting from the start position to look forward, and if it does not exist, returns -1.
'lastindex'.lastIndexOf('a'); // 14. 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 that it is intercepted from the start position until the end.
var str = 'abcdefg';str.substring(1, 4); //"bcd"str.substring(1); // "bcdefg"str.substring(-1); //"abcdefg" will be treated as 0 when passing in a negative value
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()
str.slice(start[,end])
=> Returns a substring in str from the specified position to the specified length, start 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 str's substring
var str = "do you love me";str.replace('love','hate'); // "do you hate me"8. search()
str.search(regexp)
=> Find if str matches a regular expression. If the match is successful, return the index of the first match of the regular expression in the string; otherwise, return -1. If the parameter is passed in a non-regex 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'); // 7str.search('Java'); // 7str.search('Java'); // 79. match()
str.match(regexp)
=> Returns an array containing matching results, and if there is no match, return null. If the parameter is passed in a non-regex 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", "l", "o", "?", "W", "o", "r", "l", "d", "!"]str.split('?'); // ["Hello", "World!"]str.split('',5); // ["H", "e", "l", "l", "o"]11. trim()
str.trim()
=> Remove the whitespace 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()
=> Convert str to lowercase and return a 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()
=> Convert str to uppercase and return a copy of str without affecting the value of the string itself
var str = 'JavaScript';str.toUpperCase(); // 'JAVASCRIPT'console.log(str); // 'JavaScript'
The above is a detailed explanation of the common Javascript string methods introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!