The String type is an object wrapper for a String, which can be created using the String constructor as follows.
String methods are also accessible in all basic String values. The inherited valueOf(), tolocale-String (), and toString() methods all return the base String value represented by the object.
Each instance of String has a length attribute, indicating that the String contains more than one character.
var stringValue = "hello world"; alert(stringValue.length); / / "11"Copy the code
This example prints the number of characters in the string “Hello world”, that is, “11”. It should be noted that even if the string contains double-byte characters (not one-byte ASCII characters), each character still counts as one character.
The String type provides many methods to assist in parsing and manipulating strings in ECMAScript.
1. Character methods
The two methods used to access specific characters in a string are charAt() and charCodeAt(). Both methods accept a single argument, a character position based on zero. The charAt() method returns the character at the given position as a single-character string (there are no character types in ECMAScript).
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
Copy the code
The character at position 1 of the string “hello world” is “e”, so calling charAt(1) returns “e”. If you want not characters but character encodings, use charCodeAt() as follows.
var stringValue = "hello world"; alert(stringValue.charCodeAt(1)); / / output "101"Copy the code
This example outputs “101”, which is the character code for the lowercase letter “e”.
ECMAScript 5 also defines another way to access individual characters. In browsers that support this approach, you can access specific characters in a string using square brackets and a numeric index,
var stringValue = "hello world";
alert(stringValue[1]); //"e"
Copy the code
The syntax for accessing individual characters using square brackets notation is supported by Internet Explorer 8 and all versions of Firefox, Safari, Chrome, and Opera. If you use this syntax in IE7 and earlier versions, undefined is returned (though not at all a special 6 undefined).
2. String operation methods
The first is concat(), which concatenates one or more strings and returns the concatenated new string.
var stringValue = "hello ";
var result = stringValue.concat("world"); alert(result); //"hello world" alert(stringValue); //"hello"
Copy the code
In this example, calling the concat() method through stringValue returns “Hello world” — but the value of stringValue remains the same. In fact, the concat() method can take any number of arguments, which means it can concatenate any number of strings.
var stringValue = "hello "; var result = stringValue.concat("world", "!" ); alert(result); //"hello world!" alert(stringValue); //"hello"Copy the code
This example combines “world” with “!” Concatenated to the end of “Hello”. Although concat() is a square 11 method specifically used to concatenate strings, the plus operator (+) is more commonly used in practice. Also, using the plus operator is easier than using the concat() method in most cases (especially if you concatenate multiple strings).
ECMAScript also provides three methods for creating new strings based on substrings :slice(), substr(), and substring(). 12 Each of these methods returns a substring of the string being operated on, and each takes one or two arguments. The first argument specifies where the substring starts, and the second argument (if specified) indicates where the substring ends. Specifically, the second argument to slice() and substring() specifies the position after the last character of the substring. The second argument to substr() specifies the number of characters to return. If no second argument is passed to these methods, the length of the string is used as the end position. Like the concat() method, slice(), substr(), and substring() do not modify the value of the string itself — they simply return a string value of the primitive type and have no effect on the original string.
var stringValue = "hello world"; alert(stringValue.slice(3)); alert(stringValue.substring(3)); alert(stringValue.substr(3)); alert(stringValue.slice(3, 7)); Alert (stringValue. Substring (3, 7)); alert(stringValue.substr(3, 7)); //"lo world" //"lo world" //"lo world" //"lo w" //"lo w" //"lo worl"Copy the code
This example compares the results of calling slice(), substr(), and substring() in the same way, and in most cases the results are the same. All three methods return “lo world” with only one argument, 3, because the second “L” in “hello” is in position 3. With two parameters 3 and 7, slice() and substring() return “lo w” (the “o” in “world” is in position 7, so “o” is not included in the result), but substr() returns “lo worl”, Because its second argument specifies the number of characters to return.
These methods behave differently when the parameters passed to them are negative. The slice() method adds the passed negative value to the length of the string, the substr() method adds the negative first argument to the length of the string, and converts the negative second argument to 0. Finally, the substring() method converts all negative arguments to 0.
var stringValue = "hello world"; alert(stringValue.slice(-3)); alert(stringValue.substring(-3)); alert(stringValue.substr(-3)); alert(stringValue.slice(3, -4)); alert(stringValue.substring(3, -4)); alert(stringValue.substr(3, -4)); / / "RLD" / / "hello world" / / "RLD" / / "lo w" / / "hel" / / "" (empty string)Copy the code
This example clearly demonstrates the different behaviors between the three methods. Slice () and substr() behave the same when passing a negative parameter to them. This is because -3 is converted to 8(string length plus argument 11+(3)=8), essentially equivalent to calling slice(8) and substr(8). But the substring() method returns the entire string because it converts -3 to 0.
Each of the three methods behaves differently when the second argument is negative. The slice() method converts the second argument to 7, which is equivalent to calling slice(3,7), thus returning “lo w”. The substring() method converts the second argument to 0, making the call substring(3,0), and since the method starts with a smaller number and ends with a larger number, it ends up calling substring(0,3). Substr () also converts the second argument to 0, which means that a string containing zero characters is returned, which is an empty string.
3. String position method
There are two methods to find substrings from strings :indexOf() and lastIndexOf(). Both of these methods search for a given substring from a string, and then return the location of the substring (-1 if it was not found). The difference between the two methods is that indexOf() searches substrings backwards from the beginning of the string, while lastIndexOf() searches substrings backwards from the end of the string.
var stringValue = "hello world"; alert(stringValue.indexOf("o")); //4 alert(stringValue.lastIndexOf("o")); / / 7Copy the code
The substring “o” first appears at position 4, the “O” in “hello”; It was last seen in position 7, the “O” in “world”. If “o” occurs only once in the string, indexOf() and lastIndexOf() return the same positional value.
Both methods can take an optional second argument indicating where in the string to start the search. In other words, indexOf() searches backwards from the position specified by this argument, ignoring all characters preceding that position; LastIndexOf (), on the other hand, searches forward from the specified position, ignoring all characters after that position.
var stringValue = "hello world"; alert(stringValue.indexOf("o", 6)); //7 alert(stringValue.lastIndexOf("o", 6)); / / 4Copy the code
After passing the second argument, 6, to both methods, you get the opposite result from the previous example. This time, since indexOf() searches backwards from position 6(the letter “w”), it finds “O” in position 7, so it returns 7. Last-indexof () is searched forward from position 6. It finds the “O” in “Hello”, so it returns 4. In the case of the second argument, all matching substrings can be found by looping indexOf() or lastIndexOf(), as shown in the following example:
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; var positions = new Array(); var pos = stringValue.indexOf("e"); while(pos > -1){ positions.push(pos); pos = stringValue.indexOf("e", pos + 1); } alert(positions); 3,24,32,35,52 "/ /"Copy the code
This example iterates through a long string by incrementing the location where the indexOf() method starts the search. Outside of the loop, the initial position of “e” in the string is found; After the loop, increments the previous position in indexOf() each time. This ensures that each new search starts after the substring found last time. The positions returned by each search, in turn, are stored in an array of Positions for future use.