Open Directory Site JavaScript Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> JavaScript >> JavaScript String Object

 JavaScript String Object

 

The most common methods

Methods Explanation NN IE ECMA

length Returns the length of the string 2.0 3.0 1.0

indexOf() Returns the index of the first time the specified

character occurs, or -1 if it never occurs, so with

that index you can determine if the string contains

the specified character. 2.0 3.0

lastIndexOf() Same as indexOf, only it starts from the right and

moves left. 2.0 4.0

match() Behaves similar to indexOf and lastIndexOf, but

the match method returns the specified characters,

or "null", instead of a numeric value. 4.0 4.0

substr() Returns the characters you specified: (14,7)

returns 7 characters, from the 14th character. 4.0 4.0

substring() Returns the characters you specified: (14,7)

returns all characters between the 7th and the 14th. 2.0 3.0 1.0

toLowerCase() Returns the string in lower case 2.0 3.0 1.0

toUpperCase() Returns the string in upper case 2.0 3.0 1.0

Examples

1.The length() Method

Use the length property to find out how many character there is in the string.

Coding

<html>

<body>

<script language="JavaScript">

var str="how many characters"

document.write(str.length)

</script>

</body>

</html>

Output

19

2.The indexOf() Method

Test if a string contains a specified character. Returns an integer if it does and -1 if it do not. Use this method in a form validation.

Coding

<html>

<body>

<script language="JavaScript">

var str="W3Schools"

var character=str.indexOf("School")

document.write(character + "<br>")

if (character>=0)

{

document.write("The string contains the specified character")

}

</script>

</body>

</html>

Output

2

The string contains the specified character

3.The match() Method

Works similar to the indexOf method, only this method returns the characters you specified, "null" if the string do not contain the specified characters.

Coding

<html>

<body>

<script language="JavaScript">

var str = "W3Schools are great"

document.write(str.match("great"))

</script>

</body>

</html>

Output

great

4.The substr() Method

The substr method returns specified parts of the string. If you specify (14,7) the return will be the 14th character and the next 7. Note that the first character is 0, the second is 1 etc.

Coding

<html>

<body>

<script language="JavaScript">

var str="W3Schools are great"

document.write(str.substr(3,6))

</script>

</body>

</html>

Output

chools

5. The toLowerCase() toUpperCase() Method

How to return a string in lower or upper case.

Coding

<html>

<body>

<script language="JavaScript">

var str=("Hello JavaScripters!")

document.write(str.toLowerCase())

document.write("<br>")

document.write(str.toUpperCase())

</script>

</body>

</html>

Output

hello javascripters!

HELLO JAVASCRIPTERS

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting