1. String
The code copy is as follows:
var language = "javascript";
var language = 'javascript';
Strings can be made using double and single quotes, depending on your personal preference.
A string has a length attribute, which can return the number of strings in a variable.
The code copy is as follows:
var test1 = "teacher" ;
document.write(test1.length);
//The number of strings output to test1: 7
On the contrary, if you want to get the characters at the specified position, you can use the charAt() function (the first character is 0, the second character is 1, and so on)
The code copy is as follows:
var test1 = "teacher" ;
document.write(test1.charAt(1));
//The operation result is: e,
If you want to get the string in a variable, you can use slice(), substring() or substr() functions.
Among them, substring() and slice() both accept two parameters
The code copy is as follows:
var test1 = "teacher" ;
document.write(test1.substring(1)+"<br>");// Output each
document.write(test1.substring(1,4)+"<br>"); //Output eac
document.write(test1.slice(1,4)+"<br>"); //Output eac
document.write(test1.slice(4)+"<br>"); //Output her
document.write(test1 + "<br>");//Full string
From the above content, substring() and slice() do not change the string content, and only return the content of the string.
The difference between substing() and slice() is mainly because of the different handling of negative numbers.
For slice(), counting forward from the end of the string, for substring(), negative numbers are ignored, processing starts from 0, and the smaller number among the two parameters is the start bit and the larger one is the end bit.
For example, substring(2,-3) is equivalent to substing(2,0), that is, equivalent to substring(0,2).
The code copy is as follows:
var test1 = "teacher" ;
document.write(test1.substring(2,-3)+"<br>"); //te
document.write(test1.substring(2,0)+"<br>"); //te
document.write(test1.substring(0,2)+"<br>"); //te
document.write(test1.slice(2,-3)+"<br>"); //ac
document.write(test1 + "<br>"); //teacher
The difference between substring() and substr() is given as an example.
The code copy is as follows:
var tt,ss ;
var s = "hellobeijing";
tt = s.substring(2,8)+"<br>";
ss = s.substr(2,8);
document.write(tt);//Output: llobeij outputs characters between subscript 2 and subscript 8
document.write(ss); //Output: llobeiji (Output 8 characters after subscript 2)
For usage, another blogger has more examples (address)
On the search string, Javascript provides two functions: indexof() and lastindexof().
The code copy is as follows:
var s = "woaibeijing";
dd = s.indexOf("e")+"<br>";//From the front to the back
ee = s.indexOf("e",3)+"<br>";//Optional parameters, search from the second character
ff = s.lastIndexOf("e")+"<br>";//From the back to the front
gg = s.lastIndexOf("e",3)+"<br>"; // Optional parameters, look up from the first character
hh = s.lastIndexOf("H")+"<br>";
document.write(dd);
document.write(ff);
document.write(ee);
document.write(gg);
document.write(hh);
In addition, it is recommended to view this article for the usage of indexof() and lastindexof(). //www.VeVB.COM/article/44921.htm
indexOf and lastIndexOf in JS are a very useful function for processing strings. The following is a description of their definition, usage, precautions and usage suggestions.
1. strObj.indexOf(subString[, startIndex])
Function: Returns the index value of the first character of the specified substring in the source string (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, a must-have option.
subString is a substring that is found in the source string object, a required option.
startIndex is the start index value, and the indexOf function starts searching from the character whose index value of the source string is startIndex (that is, startIndex + 1 character). It is optional. When omitted, search starts from a character whose index value of the source string is 0 (i.e., the first character).
Example analysis:
Usage 1 (not specify 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, and the following examples are the same.
Things to note
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 no substring is found, return -1.
[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 same result 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 is searched from left to right, but in actual applications, we sometimes want to get the first character index value of a character or string from right to left. In this case, JS gives another function lastIndexOf to solve this problem. The use method is similar to indexOf, but it looks from right to left. I won't repeat it in detail. I'll give a few simple examples to compare it 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. Use suggestions
To avoid unexpected results, unless there is a special purpose, it is recommended to follow the following principles:
1. The startIndex is a non-negative number and is not greater than the maximum index value. If startIndex is a variable, you must 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 to program. If the function name is written incorrectly, the function color is black, and if written correctly, it will become another color.