1. Character Method
charAt() and charCodeAt(), both methods accept a parameter and a character position based on 0. charAt() returns a character at a given location, and charCodeAt() returns a character encoding of a character at a given location. One method corresponding to charCodeAt() is formCharCode(), which converts character encoding into strings.
Such as:
<script>
var code = "future";
console.log(code.charAt(2)); //t
console.log(code.charCodeAt(2)) //116
</script>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
In addition to the above methods, you can also access characters in specific locations through square brackets, such as:
<script>
var code = "future";
console.log(code[2]); //t
</script>
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
Square bracketing is supported in IE8, Firefox,Safari,Chrome and Opera, but not in IE7 and earlier versions. The return value is undefined.
2. String operation method
1. String splicing: concact() method.
var a = "my ";
console.log(a.concat("future"));//my future
console.log(a.concat("future ", "will"));//my future will
- 1
- 2
- 3
- 1
- 2
- 3
2. Create new strings based on substrings: slice(), substr() and substring(). All three methods return a substring of the manipulation string and receive one or two parameters.
The first parameter specifies the starting position of the string, and the second parameter (omitable) indicates where the substring ends. The second parameter of slice() and substring() specifies the position after the last character of the substring, while the second parameter of substr() specifies the number of characters returned. If the second parameter is omitted, it is intercepted to the last position of the string.
var str = "We are family";
console.log(str.slice(2,5)); // AR (starting from the second position, excluding the fifth position)
console.log(str.slice(1)); //e are family (from the first position to the last)
console.log(str.substring(3)); //Are families (starting at position 3, and ending at the end)
console.log(str.substring(3,5));//ar (starting at position 3, excluding position 5)
console.log(str.substr(2)); // Are families (from the second position to the last)
console.log(str.substr(2,6)); // Aref (from the second position, intercept 6 characters)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
slice() and substring() methods achieve the same function in the positive case, but the behavior is different when the passed value is negative.
The slice() method adds the incoming negative value to the length of the string (therefore, if both parameters are negative, the second parameter must be larger than the first parameter, or the empty string will be returned)
The substr() method adds the length of the string to the negative first parameter and converts the second parameter to zero. (Returns an empty string if the second parameter is negative)
substring() converts all negative parameters to zero, and substring always takes smaller parameters as the starting position.
var str = "Wearefamily"; //length is 11.
console.log(str.slice(-3,-8));//Equivalent to str.slice(8,3)// Returns an empty string
console.log(str.slice(-5,-1));//Equivalent to str.slice(6,10)// return to ami
console.log(str.substring(3,-4));//Equivalent to str.substring(3,0)// Return Wea
console.log(str.substring(-3,-5));//Equivalent to str.substring(0,0)// returning an empty string
console.log(str.substr(-4,3));//Equivalent to substr(7,3)// returning mil
console.log(str.substr(-4,-3));//Equivalent to substr(7,0)// returning an empty string
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3. The Position Method of Strings
String location methods: indexOf() and lastIndexOf(). Both methods search for a given substring from a string, and then return to the position of the substring, which is not found, returning - 1.indexOf() to search for the substring backwards from the beginning of the string, and lastIndexOf() to search for the substring forward from the end of the string.
var str = "WEAREFAMILY";
console.log(str.indexOf("A"));//2
console.log(str.lastIndexOf("A"));//6
- 1
- 2
- 3
- 1
- 2
- 3
4.trim() method
trim() is used to remove the space at the end of the string
5. Conversion of string case
Convert to uppercase: toUpperCase() and toLocalUpperCase()
Convert to lowercase: toLowerCase() and toLocalLowerCase()
6. Pattern matching of strings
The match(),replace(),search(),replace(), and split() methods are several methods that are used to match patterns in strings centrally defined by String types.
The match method takes only one parameter, either a regular expression or a RegExp object. Calling the match() method on a string is essentially the same as calling RegExp's exec() method, returning an array
var str = "we are weamy";
var pattern = /we/;
var matches = str.match(pattern);
console.log(matches);//["we", index: 0, input: "we are weamy"]
console.log(pattern.exec(str));//["we", index: 0, input: "we are weamy"]
console.log(matches.index);//0
console.log(matches[0]);//we
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
The parameters of search() are the same as match(), except that search() returns the index of the first match in the string; if no match is found, it returns - 1.
The replace() method takes two parameters. The first parameter can be a RegExp object or a string. The second parameter can be a string or a function. If the first parameter is a string, only the first string will be replaced. If you want to replace all strings, you must use regular expressions and specify the global flag (g).
var str = "cat, fat, sat, bat";
console.log(str.replace("at","ooo"));//cooo, fat, sat, bat
console.log(str.replace(/at/g,"ooo"));//cooo, fooo, sooo, booo
- 1
- 2
- 3
- 1
- 2
- 3
If the second parameter of replace() is a string, you can also use some special character sequence to insert the value of the regular expression operation into the result character.
$$ $
S & Substring matching the entire pattern
Sn matches the substring of the nth capture group
var str = "cat, fat, sat, bat";
console.log(str.replace("at","ooo"));//cooo, fat, sat, bat
console.log(str.replace(/(.at)/g,"ooo ($1)"));//ooo (cat), ooo (fat), ooo (sat), ooo (bat)
console.log(str.replace(/(.at)/g,"ooo ($&)"));//ooo (cat), ooo (fat), ooo (sat), ooo (bat)
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
The split() method takes a parameter as a delimiter and converts the string into an array.
The corresponding method is join(), which is used to convert arrays into strings.
7. LocalCompare() method
This method is used for string comparison.
If the string is in the alphabet before the string parameter should be taken, a negative number is returned. (In most cases - 1, the specific value depends on the implementation)
If the string equals the string parameter, 0 is returned.
If the order of strings in the alphabet is after the string parameters, a positive number is returned. (In most cases, 1, the specific value depends on the implementation)
var str = "break";
console.log(str.localeCompare("future")); //-1
console.log(str.localeCompare("above")); //1
console.log(str.localeCompare("break")); //0