Home > Article > Web Front-end > Detailed explanation of string operations in JavaScript_javascript skills
1. Overview
Strings are almost everywhere in JavaScript. When you process user input data, when you read or set the properties of a DOM object, when you operate cookies, and of course there are more .... The core part of JavaScript provides a set of properties and methods for common string operations, such as splitting strings, changing the case of strings, operating substrings, etc.
Most current browsers can also benefit from the power of regular expressions, because it greatly simplifies a large number of string manipulation tasks, but it also requires you to overcome a somewhat steep learning curve. Here, we mainly introduce some operations on the string itself. Regular expressions will be covered in future essays.
2. Creation of String
There are several ways to create a string. The simplest is to enclose a set of characters in quotes, which can be assigned to a string variable.
var myStr = "Hello, String!";
You can use double quotes or single quotes to enclose the string, but be aware that the pair of quotes that delimit the string must be the same and cannot be mixed.
A declaration like var myString = "Fluffy is a pretty cat.'; is illegal.
Allows the use of two kinds of quotation marks, making certain operations simple, such as embedding one into the other:
We created several strings in the above script, but in essence, they are not real string objects. To be precise, they are string type values. To create a string object, you can use the following statement: var strObj = new String("Hello, String!");
Using the typeof operator, you will find that the myStr type above is string, and the strObj type is object.
If you want to know the length of a string, use its length property: string.length.
Method to get the character at the specified position of the string: string.charAt(index);
3. String concatenation
Problem:
Splicing two or more strings into one large string
Solution:
Very simple, just use a " " to "Add" two strings:
String object also provides the method concat(), which performs the same function as " ":
string.concat(value1, value2, ...)
However, the concat() method is obviously not as good as " " Intuitive and concise.
4. Accessing substrings of strings
Question:
Obtain a copy of a part of a string.
Solution:
Use substring() or slice() method (NN4, IE4), their specific usage is explained below.
The prototype of substring() is: string.substring(from, to)
The first parameter from specifies the starting position of the substring in the original string (0-based index); the second parameter to is optional. It specifies the end position of the substring in the original string (0-based index). Generally, it should be larger than from. If it is omitted, the substring will go all the way to the original string. at the end.
What will happen if the parameter from is accidentally larger than the parameter to? JavaScript will automatically adjust the starting and ending positions of the substring, that is, substring() always starts from the smaller of the two parameters and ends with the larger one. Note, however, that it includes the character at the starting position, but not the character at the ending position.
The prototype of slice() is: string.slice(start, end)
The parameter start represents the starting position of the substring. If it is a negative number, it can be understood as the number from the last to the beginning. For example, -3 means starting from The third from the bottom starts; the parameter end represents the end position. Like start, it can also be a negative number, and its meaning also indicates the end of the penultimate position. The parameters of slice() can be negative, so it is more flexible than substring(), but less tolerant. If start is larger than end, it will return an empty string (example omitted).
Another method is substr(), whose prototype is: string.substr(start, length)
From the prototype, you can see the meaning of its parameters. start represents the starting position, and length represents the substring. length. The JavaScript standard discourages the use of this method.
5. String case conversion
Problem:
There is a text box on your page to receive user input information, such as city, and then you will do different processing according to his city. String comparison will naturally be used at this time, so before comparison, it is best to perform case conversion, so that you only need to consider the situation after conversion; or you need to collect data on the page, and then store the data in the database, and the database It happens that only uppercase characters are accepted; in these cases we all need to consider case conversion of the string.
Solution:
Use toLowerCase() and toUpperCase() methods:
There is one thing you need to pay attention to, which is the computer’s regional settings. If you use "<" and ">" to compare strings, then JavaScript compares them as Unicode, but obviously people browsing the web don't read text as Unicode :) For example in Spanish, According to traditional sorting, "ch" will be sorted as a character between "c" and "d". localeCompare() provides a way to use the character collation of the default locale.
7. Searching for strings
Question:
Determine whether a string contains another string.
Solution:
Use the indexOf() method of string:
strObj.indexOf(subString[, startIndex])
strObj is the string to be judged, subString is the substring to be found in strObj String, startIndex is optional, indicating the starting position of the search (0-based index), if startIndex is omitted, search from the beginning of strObj, if startIndex is less than 0, then start from 0, if startIndex is greater than the maximum index, then search from Start at the maximum index.
indexOf() returns the starting position of subString in strObj, if not found, returns -1. In a script, you can use it like this:
8. Convert between Unicode values and characters in strings
Question:
Obtain the Unicode encoding value of a character and vice versa.
Solution:
To obtain the Unicode encoding of a character, you can use the string.charCodeAt(index) method, which is defined as:
strObj.charCodeAt(index)
index is the specified character in the strObj object position (0-based index), the return value is a 16-bit integer between 0 and 65535. For example:
var strObj = "ABCDEFG";
var code = strObj.charCodeAt(2); // Unicode value of character 'C' is 67
If there is no character at the index specified by index, the return value is NaN.
To convert Unicode encoding to a character, use the String.fromCharCode() method. Note that it is a "static method" of the String object, which means you do not need to create a string instance before use:
String. fromCharCode(c1, c2, ...)
It accepts 0 or more integers and returns a string that contains the characters specified by each parameter, for example:
Reference:
JavaScript And Dhtml Cookbook(Oreilly) ;
JavaScript-The Definitive Guide(4th Edition);