WHAT'S NEW?
Loading...

Using string - Split(), substr(), substring(), toLocaleLowerCase(), toLocaleUppereCase() in javascript with example - Tech-n-Savvy Blogger


split ()

This method splits a String object into an array of strings by separating the string into substrings.

Syntax

Its syntax is as follows:

string.split([separator][, limit]);

Argument Details

·     separator : Specifies the character to use for separating the string. If separator is omitted, the array returned contains one element consisting of the entire string.

·         limit : Integer specifying a limit on the number of splits to be found.

Return Value

The split method returns the new array. Also, when the string is empty, split returns an array containing one empty string, rather than an empty array.

Example

Try the following example.

<html>

<head>

<title>JavaScript String split() Method</title>

</head>

<body>

<script type="text/javascript">


var str = "Apples are round, and apples are juicy."; var splitted = str.split(" ", 3); document.write( splitted );


</script>

</body>

</html>

Output

Apples,are,round,


substr ()

This method returns the characters in a string beginning at the specified location through the specified number of characters.

Syntax

The syntax to use substr() is as follows:

string.substr(start[, length]);

Argument Details

·         start : Location at which to start extracting characters (an integer between 0 and one less than the length of the string).

·         length : The number of characters to extract.

Note: If start is negative, substr uses it as a character index from the end of the string.

Return Value

The substr() method returns the new sub-string based on given parameters.

Example

Try the following example.

<html>

<head>

<title>JavaScript String substr() Method</title>

</head>

<body>

<script type="text/javascript">


var str = "Apples are round, and apples are juicy.";


document.write("(1,2): "           + str.substr(1,2));

document.write("<br />(-2,2): "        + str.substr(-2,2));

document.write("<br />(1): "                + str.substr(1));

document.write("<br />(-20, 2): " + str.substr(-20,2));

document.write("<br />(20, 2): "     + str.substr(20,2));


</script>

</body>

</html>

Output

(1,2): pp

(-2,2): y.

(1): pples are round, and apples are juicy.

(-20, 2): nd

(20, 2): d


substring ()

This method returns a subset of a String object.

Syntax

The syntax to use substr() is as follows:

string.substring(indexA, [indexB])

Argument Details


·         indexA : An integer between 0 and one less than the length of the string.

·         indexB : (optional) An integer between 0 and the length of the string.

Return Value

The substring method returns the new sub-string based on given parameters.

Example

Try the following example.

<html>

<head>

<title>JavaScript String substring() Method</title>

</head>

<body>

<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

document.write("(1,2): "           + str.substring(1,2));

document.write("<br />(0,10): "        + str.substring(0, 10));

document.write("<br />(5): "                + str.substring(5));

</script>

</body>

</html>

Output

(1,2): p

(0,10): Apples are

(5): s are round, and apples are juicy.


toLocaleLowerCase()

This method is used to convert the characters within a string to lowercase while respecting the current locale. For most languages, it returns the same output as toLowerCase.

Syntax

Its syntax is as follows:

string.toLocaleLowerCase( )

Return Value

Returns a string in lowercase with the current locale.






Example

Try the following example.

<html>

<head>

<title>JavaScript String toLocaleLowerCase() Method</title>

</head>

<body>

<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy."; document.write(str.toLocaleLowerCase( ));

</script>

</body>

</html>

Output

apples are round, and apples are juicy.


toLocaleUppereCase ()

This method is used to convert the characters within a string to uppercase while respecting the current locale. For most languages, it returns the same output as toUpperCase.

Syntax

Its syntax is as follows:

string.toLocaleUpperCase( )

Return Value

Returns a string in uppercase with the current locale.


Example

Try the following example.

<html>

<head>

<title>JavaScript String toLocaleUpperCase() Method</title>

</head>

<body>

<script type="text/javascript">

var str = "Apples are round, and Apples are Juicy."; document.write(str.toLocaleUpperCase( ));

</script>

</body>

</html>

Output

APPLES ARE ROUND, AND APPLES ARE JUICY.

0 comments:

Post a Comment