Home >Web Front-end >JS Tutorial >Javascript Basic Tutorial Data Type (String)_Basic Knowledge
1. String
Strings can use double quotes and single quotes, depending on personal preference.
String has the length attribute, which can return the number of strings in the variable.
On the contrary, if you want to get the character at the specified position, you can use the charAt() function (the first character is 0, the second character is 1, and so on)
If you want to get the string in the variable, you can use the slice(), substring() or substr() function.
Among them, substring() and slice() both accept two parameters
From the above content, it can be seen that substring() and slice() do not change the content of the string, but only return the content of the string.
The difference between substing() and slice() is mainly the handling of negative numbers.
For slice(), negative numbers are counted from the end of the string. For substring(), negative numbers are ignored, processing starts from 0, and the smaller number of the two parameters is used as the starting point. bit, the larger one serves as the end bit.
For example, substring(2,-3) is equivalent to substing(2,0), which is equivalent to substring(0,2).
The difference between substring() and substr(), give an example.
For usage, another blogger has more examples (address)
On the search string, Javascript provides two functions, indexof() and lastindexof().
In addition, it is recommended to read this article for the usage of indexof() and lastindexof(). http://www.jb51.net/article/44921.htm
indexOf and lastIndexOf in JS are very useful functions for processing strings. Their definition, usage, precautions and usage suggestions are introduced below.
1. strObj.indexOf(subString[, startIndex])
Function: Returns the index value of the first character in the source string where the specified substring appears for the first time (the index value of the nth character in the source string is n-1), which is an integer .
Parameter meaning:
strObj is the source string, required.
subString is the substring found in the source string object, required.
startIndex is the starting index value, and the indexOf function starts searching from the character whose index value is startIndex (that is, the 1st character of startIndex) in the source string. It is optional. When omitted, the search starts from the character with index value 0 in the source string (that is, the first character).
Case analysis:
Usage 1 (without specifying startIndex value): var i="huoshandao.com".indexOfOf("a"): then i=5
Equivalent to var i="huoshandao.com".indexOf("a",0)
Usage 2 (specify startIndex value): var i="huoshandao.com".indexOf("a",6): then i=8
Tip: You can use the alert(i); statement to test the results, as in the following example.
Notes
1) strObj can be either a string or a string variable.
[Example]
strObj is a string: var i="huoshandao.com".indexOf("."):
strObj is a string variable: var str="huoshandao.com";var i=str.indexOf(".");
2) subString cannot be an empty string. If it is an empty string, the return value is 0, but it can be a space.
[Example]
SubString is an empty string: var i="huo shan dao".indexOf(""): then i=0
SubString is a space string: var i="huo shan dao".indexOf(" "): then i=3
3) The startIndex value of the first character is 0, which is the minimum index value; the startIndex value of the second character is 1; the startIndex value of the last character is the source string length minus 1, which is the maximum index value.
4) If the substring is not found, -1 is returned.
[Example]
var i="huoshandao.com".indexOf("huosan"): Then i=-1
5) If startIndex is a negative number, it is equivalent to the case where startIndex is equal to 0. If it is greater than the maximum index value, it is equivalent to the case where startIndex is equal to the maximum index value.
[Example]
StartIndex is a negative number: var i="huoshandao.com".indexOf(".",-3); then i=10
The result is the same as var i="huoshandao.com".indexOf(".",0);
StartIndex is greater than or equal to the string length: var i="huoshandao.com_".indexOf("_",16); then i=-1
With var i="huoshandao.com_".indexOf("_",14);i=14
2. strObj.lastIndexOf(subString[, startIndex])
The indexOf function searches from left to right, but in actual applications we sometimes want to get the first character index value of a character or string that appears from right to left. For this situation, JS provides another function lastIndexOf. To solve this problem, the usage method is similar to indexOf, except that it searches from right to left. The details will not be repeated. Here are a few examples to compare with indexOf:
Example 1: var i="huo.shan.dao.com".lastIndexOf("."); then i=12 and var i="huo.shan.dao.com".indexOf(".") ;i=3
Example 2: var i="huoshandao.com_".lastIndexOf("_",16); then i=14 and var i="huoshandao.com_".indexOf("_",16); then i=-1
3. Usage suggestions
In order to avoid unexpected results, unless there is a special purpose, it is recommended to follow the following principles:
1. startIndex is a non-negative number and not greater than the maximum index value. If startIndex is a variable, first determine whether its value is within this range.
2. If the substring subString is a variable, you must first determine whether it is empty and then use the indexOf or lastIndexOf function.
3. When entering substrings, pay special attention to the difference between full-width characters and half-width characters.
4. Pay attention to the case in indexOf and lastIndexOf. JS is very sensitive to case. It is recommended to use Dreamweaver for programming. If the function name is written in the wrong case, the color of the function will be black. If it is written correctly, it will turn into another color.