String objects in Javascript

  • 2020-03-30 02:09:57
  • OfStack

The string string object is one of the built-in objects provided by Javascript.

Note in particular that the first character in the string is bit 0 and the second is bit 1.

Method to create a string object

[var] String object instance name = new String(String)

Or var String object instance name = String value


Var STR = "Hello World";

Var str1 = new String("This is a String ");

2. A String of attributes

Length: returns the length of a string

Var intlength = STR. Length //intlength = 11

3. The method of the String

CharAt (*): returns a single character in the * th bit of the string

Var x = "abcdefg"; Var y = x.c harAt (3); / / y = "d"

CharCodeAt (*): returns the ASCII code for a single character in the * th bit of the string

Don't go into
fromCharCode():  Accept a specified Unicode Value and then returns a string.  

document.write(String.fromCharCode(72,69,76,76,79)); //The output is HELLO

indexOf(): Find another string object from the string, find the successful return location, otherwise return -1 

document.write("children".indexOf("l",0)); //The output is 3

document.write("children".indexOf("l",1)); //The output is 3

document.write("children".indexOf("l",4)); //The output is minus 1

lastIndexOf(): and indexOf() The method is similar, except that the search direction is opposite, from the back to the front  

document.write("children".lastIndexOf("l",4)); //The output is 3

Split (separator character): returns an array that is separated from the string and the separator character determines where to split.

'l&o & v&e. Split (' &'); // returns the array l,o,v,e

Substring (): equivalent to string clipping

The substring (< The beginning > [, < end >] )

Document. The write (" children ". The substring (1, 3)); // the output is hil

Substr (): also equivalent to tailoring, note the difference from substring()

Substr (< The beginning > [, < length >] )
document.write("children".substr(1,3)); //The output is hil. Note that compared to substing, the result is the same, but the algorithm and idea are different.

toLowerCase() and toUpperCase(): The function is similar except that it returns the same string as the original string, with the only difference being that all letters are lowercase and all letters are uppercase.  

document.write("LOVE".toLowerCase()); //The output is love

document.write("love".toUpperCase()); //The output is LOVE

Related articles: