1, Preface:
The common operation methods of strings can be summarized as add, delete, modify and query. You need to know that once a string is created, it is immutable.
2, Increase
The meaning of adding is not to add content directly, but to create a copy of the string and then operate it. In addition to string splicing with + and ${}, you can also use concat.
1. concat() is used to splice one or more strings into a new string
<script> function demoConcat() { var str = "hello "; var ret = str.concat("world"); console.log(ret); //hello world console.log(str); //hello } demoConcat() </script>
3, Delete
Delete does not mean to delete the contents of the original string, but to create a copy of the string and then operate.
1. slice() method extracts a part of a string and returns a new string without changing the original string (taking the head but not the tail)
<script> function demoSlice() { var str = 'I hope the sea is calm,But there are often strong winds and bad waves.' console.log(str.slice(1, 8)); // Output: hope the sea is calm console.log(str.slice(4, -2)); // Output: the wind is calm, but there are often strong winds and evil console.log(str.slice(12)); // Output: there are strong winds and bad waves. console.log(str.slice(30)); // Output: '' } demoSlice() </script>
2. The substring() method returns a subset of a string from the start index to the end index, or a subset from the start index to the end of the string (including the head but not the tail)
<script> function demoSubstring() { var str = "I am willing to give up those young frivolous for you"; console.log(str.substring(0, 3)); //I do console.log(str.substring(3, 7)); //Give up for you console.log(str.substring(3, 3)); //"" console.log(str.substring(0, 15)); //I am willing to give up those young frivolous for you } demoSubstring() </script>
4, Change
The meaning of change is not to change the original string, but to create a copy of the string and then operate
1. replace() replaces the first place where , and , replaceAll() replace all
<script> function demoReplace() { var str = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"; //Replace the first dog with monkey // console.log(str.replace('dog', 'monkey')); //Replace the first dog with Li Qin var newStr = str.replace("dog", function (replacement) { return "Li Qin" }) console.log(newStr); //The quick brown fox jumps over the lazy Li Qin //If the dog reacted, was it really lazy? // Replace all dog s with Li Qin var newStr2 = str.replaceAll("dog", function (replacement) { return "Li Qin" }) console.log(newStr2); //The quick brown fox jumps over the lazy Li Qin //If the Li Qin reacted, was it really lazy? } demoReplace() </script>
2. The split() method uses the specified delimiter String to divide a String object into an array of substrings
<script> function demoSplit() { var str = 'The quick brown fox jumps over the lazy dog.'; var words = str.split(' '); console.log(words); //['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.'] //Splits the target string into an array of each character var chars = str.split(''); console.log(chars); //['T', 'h', 'e', ' ', 'q', ...'d', 'o', 'g', '.'] //Split str into an array of length 1. The only element is str itself var strCopy = str.split(); console.log(strCopy); // ["The quick brown fox jumps over the lazy dog."] //Take the first five elements of the split result chars = str.split('', 5); console.log(chars); //['T', 'h', 'e', ' ', 'q'] } demoSplit() </script>
3. Trim the blank space at the beginning and end of the string ({trim())
Trim off the blank space at the end of the string ()
Trim off the blank space in the string header, {trimStart()
4. toUpperCase() and toLowerCase() convert case
<script> function demoToCase() { var sentence = 'The quick brown fox jumps over the lazy dog.'; console.log(sentence.toUpperCase()); //THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. console.log(sentence.toLowerCase()); //the quick brown fox jumps over the lazy dog. } demoToCase() </script>
5, Check
In addition to obtaining the value of the string by index, you can also use:
1. The charAt() method returns the specified character from a string
<script> function demoCharAt() { var str = "I am willing to give up those young frivolous for you"; console.log(str.length); //15 for (let i = 0; i < str.length; i++) { console.log(str.charAt(i)); } } // demoCharAt() </script>
2. The indexOf() method returns the index of the specified value for the first time in the String object calling it, and searches from fromIndex. If the value is not found, - 1 is returned.
<script> function demoIndexOf() { var str = "I am willing to give up those young frivolous for you"; var result = str.indexOf("you"); console.log(result); //4 var ret = str.indexOf("you", 5); console.log(ret); //9 } demoIndexOf() </script>
3. The includes() method is used to judge whether a string is contained in another string, and returns true or false according to the situation.
<script> function demoIncludes() { var str = "I hope the sea is calm,But there are often strong winds and bad waves." console.log(str.includes("Langjing"));//true console.log(str.includes("Shanghai"));//false console.log(str.indexOf("Langjing") != -1);//true console.log(str.indexOf("Shanghai") != -1);//false } demoIncludes() </script>