WHAT'S NEW?
Loading...

Using String properties, charAt, charCodeAt, concat, indexof, lastindexof in javascript with example - Tech-n-Savvy Blogger


The String object lets you work with a series of characters; it wraps Javascript's string primitive data type with a number of helper methods.

As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.

Syntax

Use the following syntax to create a String object:


var val = new String(string);

The string parameter is a series of characters that has been properly encoded.

String Properties

Here is a list of the properties of String object and their description.

Property
Description




constructor
Returns  a  reference  to  the  String  function  that  created  the

object.


Length
Returns the length of the string.


prototype
The  prototype  property  allows  you  to  add  properties  and

methods to an object.




In the following sections, we will have a few examples to demonstrate the usage of String properties.

constructor

A constructor returns a reference to the string function that created the instance's prototype.

Syntax

Its syntax is as follows:


string.constructor

Return Value

Returns the function that created this object's instance.

Example

Try the following example.


<html>

<head>

<title>JavaScript String constructor property</title>

</head>

<body>

<script type="text/javascript">

var str = new String( "This is string" ); document.write("str.constructor is:" + str.constructor);

</script>

</body>

</html>

Output


str.constructor is:function String() { [native code] }


Length

This property returns the number of characters in a string.

Syntax

Use the following syntax to find the length of a string:


string.length

Return Value

Returns the number of characters in the string.

Example


Try the following example.

<html>

<head>

<title>JavaScript String length Property</title>

</head>

<body>

<script type="text/javascript">

var str = new String( "This is string" ); document.write("str.length is:" + str.length);

</script>

</body>

</html>

Output

str.length is:14


Prototype

The prototype property allows you to add properties and methods to any object (Number, Boolean, String, Date, etc.).

Note: Prototype is a global property which is available with almost all the objects.

Syntax

Its syntax is as follows:

object.prototype.name = value

Example

Try the following example.

<html>

<head>

<title>User-defined objects</title>


<script type="text/javascript">


function book(title, author){

this.title = title;

this.author     = author;

}

</script>

</head>

<body>

<script type="text/javascript">

var myBook = new book("Perl", "Mohtashim"); book.prototype.price = null; myBook.price = 100;

document.write("Book title is : " + myBook.title + "<br>");

document.write("Book author is : " + myBook.author + "<br>");

document.write("Book price is : " + myBook.price + "<br>");

</script>

</body>

</html>

Output


Book title is : Perl

Book author is : Mohtashim

Book price is : 100


String Methods

Here is a list of the methods available in String object along with their description.

Method
Description


charAt()
Returns the character at the specified index.


charCodeAt()
Returns a number indicating the Unicode value of the

character at the given index.



concat()
Combines  the  text  of  two  strings  and  returns  a  new


string.




indexOf()
Returns the index within the calling String object of the


first occurrence of the specified value, or -1 if not found.




lastIndexOf()
Returns the index within the calling String object of the


last occurrence of the specified value, or -1 if not found.




localeCompare()
Returns a number indicating whether a reference string


comes before or after or is the same as the given string


in sorted order.




match()
Used to match a regular expression against a string.




replace()
Used to find a match between a regular expression and


a string, and to replace the matched substring with a


new substring.




search()
Executes  the  search  for  a  match  between  a  regular


expression and a specified string.




slice()
Extracts a section of a string and returns a new string.




split()
Splits  a  String  object  into  an  array  of  strings  by


separating the string into substrings.




substr()
Returns  the  characters  in  a  string  beginning  at  the


specified  location  through  the  specified  number  of


characters.




substring()
Returns the characters in a string between two indexes


into the string.




toLocaleLowerCase()
The characters within a string are converted to lower


case while respecting the current locale.




toLocaleUpperCase()
The characters within a string are converted to upper


case while respecting the current locale.




toLowerCase()
Returns the calling string value converted to lower case.




toString()
Returns a string representing the specified object.









toUpperCase()
Returns the calling string value converted to uppercase.




valueOf()
Returns the primitive value of the specified object.






In the following sections, we will have a few examples to demonstrate the usage of String methods.

charAt()

charAt() is a method that returns the character from the specified index.

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.length 1.

Syntax

Use the following syntax to find the character at a particular index.


string.charAt(index)

Argument Details

index: An integer between 0 and 1 less than the length of the string.

Return Value

Returns the character from the specified index.

Example

Try the following example.


<html>

<head>

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

</head>

<body>

<script type="text/javascript">

var str = new String( "This is string" ); document.writeln("str.charAt(0) is:" + str.charAt(0)); document.writeln("<br />str.charAt(1) is:" + str.charAt(1)); document.writeln("<br />str.charAt(2) is:" + str.charAt(2));
document.writeln("<br />str.charAt(3) is:" + str.charAt(3)); document.writeln("<br />str.charAt(4) is:" + str.charAt(4)); document.writeln("<br />str.charAt(5) is:" + str.charAt(5));

</script>

</body>

</html>

Output


str.charAt(0) is:T

str.charAt(1) is:h

str.charAt(2) is:i

str.charAt(3) is:s

str.charAt(4) is:

str.charAt(5) is:i


charCodeAt ()

This method returns a number indicating the Unicode value of the character at the given index.

Unicode code points range from 0 to 1,114,111. The first 128 Unicode code points are a direct match of the ASCII character encoding. charCodeAt() always returns a value that is less than 65,536.

Syntax

Use the following syntax to find the character code at a particular index.


string.charCodeAt(index)

Argument Details

index: An integer between 0 and 1 less than the length of the string; if unspecified, defaults to 0.



Return Value

Returns a number indicating the Unicode value of the character at the given index. It returns NaN if the given index is not between 0 and 1 less than the length of the string.

Example


Try the following example.


<html>

<head>

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

</head>

<body>

<script type="text/javascript">

var str = new String( "This is string" ); document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5));
</script>

</body>

</html>

Output


str.charCodeAt(0) is:84

str.charCodeAt(1) is:104

str.charCodeAt(2) is:105

str.charCodeAt(3) is:115

str.charCodeAt(4) is:32

str.charCodeAt(5) is:105





contact ()

This method adds two or more strings and returns a new single string.

Syntax

Its syntax is as follows:

string.concat(string2, string3[, ..., stringN]);

  
Argument Details

string2...stringN: These are the strings to be concatenated.

Return Value

Returns a single concatenated string.

Example

Try the following example.

<html>

<head>

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

</head>

<body>

<script type="text/javascript">

var str1 = new String( "This is string one" ); var str2 = new String( "This is string two" ); var str3 = str1.concat( str2 );

document.write("Concatenated String :" + str3);

</script>

</body>

</html>

Output

Concatenated String :This is string one This is string two


indexOf ()

This method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax

Use the following syntax to use the indexOf() method.

string.indexOf(searchValue[, fromIndex])


Argument Details

·         searchValue: A string representing the value to search for.

·         fromIndex: The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Return Value

Returns the index of the found occurrence, otherwise -1 if not found.

Example

Try the following example.

<html>

<head>

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

</head>

<body>

<script type="text/javascript">

var str1 = new String( "This is string one" ); var index = str1.indexOf( "string" ); document.write("indexOf found String :" + index );


document.write("<br />");


var index = str1.indexOf( "one" ); document.write("indexOf found String :" + index );


</script>

</body>

</html>

Output


indexOf found String :8

indexOf found String :15




lastIndexOf ()

This method returns the index within the calling String object of the last occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax

Its syntax is as follows:

string.lastIndexOf(searchValue[, fromIndex])

Argument Details

·         searchValue : A string representing the value to search for.

·         fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Return Value

Returns the index of the last found occurrence, otherwise -1 if not found.

Example

Try the following example.

<html>

<head>

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

</head>

<body>

<script type="text/javascript">

var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); document.write("lastIndexOf found String :" + index );


document.write("<br />");


var index = str1.lastIndexOf( "one" ); document.write("lastIndexOf found String :" + index );


</script>





</body>

</html>

Output

lastIndexOf found String :29


lastIndexOf found String :15

0 comments:

Post a Comment