Home > Web Front-end > JS Tutorial > Detailed discussion of String object in Javascript_javascript skills

Detailed discussion of String object in Javascript_javascript skills

WBOY
Release: 2016-05-16 16:57:17
Original
1008 people have browsed it

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

Pay special attention here, the first character in the string is the 0th character, and the second character is the 1st character.

1. Method to create 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 property

length: Returns the length of the string

var intlength = str.length //intlength = 11

3.String method

charAt(*): Returns the single character at the *th position of the string

var x = "abcdefg"; var y = x.charAt(3); //y="d"

charCodeAt(*): Returns the ASCII code of a single character in the *th position of the string

No further description

Copy code The code is as follows:

fromCharCode(): Accepts a specified Unicode value and returns a string.

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

indexOf(): Find another character from the string String object, the position is returned if the search is successful, 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, except that the search direction is opposite, searching from back to front

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

split (separator character): Returns an array. The array is separated from the string. The separator character determines where to separate. .

'l&o&v&e'.split('&'); //Returns the array l,o,v,e

substring(): Equivalent to the cutting function of string

substring([,])

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

substr(): also equivalent to cropping, please note the difference with substring()

substr([,])
Copy code The code is as follows:

document.write("children".substr(1,3)); //The output result is hil. It should be noted here that compared with substing, although the results are the same, the algorithms and ideas are different.

toLowerCase() and toUpperCase(): have similar functions, except that they return a string with the same original string. The only difference is that all letters in the former are lowercase and in the latter are uppercase.

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

document.write("love".toUpperCase()); //The output result is LOVE
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template