This article mainly introduces the String object in Javascript. If you need it, you can refer to the Sting String object, which is one of the built-in objects provided by Javascript.
Note that the first character in the string is 0th characters, and the second character is 1st characters.
1. method for creating a String object
[Var] String object instance name = new String (string)
Or var String object instance name = String value
Example:
Var str = "Hello World ";
Var str1 = new String ("This is a string ");
2. String attributes
Length: returns the length of the string.
Var intlength = str. length // intlength = 11
3. String Method
CharAt (*): returns a single character whose character string is at the * bit.
Var x = "abcdefg"; var y = x. charAt (3); // y = "d"
CharCodeAt (*): returns the ASCII code of a single character whose character string is * characters.
Do not repeat
The Code is as follows:
FromCharCode (): receives a specified Unicode value and returns a string.
Document. write (String. fromCharCode (, 79); // The output result is HELLO.
IndexOf (): searches for another string object from the string, and returns the position after successful search. Otherwise,-1 is returned.
Document. write ("children". indexOf ("l", 0); // The output result is 3.
Document. write ("children". indexOf ("l", 1); // The output result is 3.
Document. write ("children". indexOf ("l", 4); // The output result is-1.
LastIndexOf (): similar to the indexOf () method, the difference is that the query direction is the opposite.
Document. write ("children". lastIndexOf ("l", 4); // The output result is 3.
Split (delimiter character): returns an array. The array is separated from the string, and the separator character determines the separation.
'L & o & v & e'. split ('&'); // returns the array l, o, v, e
Substring (): equivalent to the string cropping function.
Substring ( <始> [, <终> ])
Document. write ("children". substring (1, 3); // The output result is hil.
Substr (): it is also equivalent to cropping. Note that it is different from substring ().
Substr ( <始> [, <长度> ])
The Code is as follows:
Document. write ("children". substr (1, 3); // The output result is hil. In comparison with substing, though the results are the same, the algorithms and ideas are different.
ToLowerCase () and toUpperCase (): functions are similar, but returns a string with the same original string. The only difference is that all the letters of the former are lower-case and the latter is upper-case.
Document. write ("LOVE". toLowerCase (); // The output result is love.
Document. write ("love". toUpperCase (); // The output result is LOVE.