String object works with series of characters. It is used to store and manipulate text.
There are two ways to create string in JavaScript:
- String literal.
- Using new keyword.
String literal
By using double quotes string literal is created.
Syntax
1 2 3 |
var stringname="string value"; |
Example
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html> <body> <script> var str="Hello String Literal"; document.write(str); </script> </body> </html> |
Output
1 2 3 |
Hello String Literal |
Using new keyword
Syntax
1 2 3 |
var stringname=new String("new keyword") |
Example
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html> <body> <script> var stringname=new String("Hello String"); document.write(stringname); </script> </body> </html> |
Output
1 2 3 |
Hello String |
String properties
There are list of properties of string object and there description.
- constructor.
- length.
- prototype.
Constructor– It returns a reference to the string function that created the object.
Syntax
1 2 3 |
string.constructor |
Length-It returns the number of characters in a string.
Syntax
1 2 3 |
string.length |
Prototype– It allowed to add properties and method to any object (Number, Boolean, string, and data etc.).
This is global property which is available most of the objects.
Syntax
1 2 3 |
object.prototype.name = value |
There are methods also available in JavaScript string:
Methods | Description |
---|---|
charAt(index) | It returns the character at the given index. |
concat(str) | It merges the text of two string and returns new string. |
indexOf(str) | It returns the index position of the given string. |
iastindexOf(str) | It returns the last index position of the given string. |
match() | It is used to match regular expression against a string. |
tolowercase() | It is used to return given string value converted to lower case. |
touppercase() | It is used to return the given string value converted to uppercase. |
valueOf() | Returns the primitive value of the specified object. |
charAt()
It is a method that returns the character from specified index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <body> <script> var str = new String( "Hello" ); 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)); </script> </body> </html> |
concat(str)
This method add two or more string and return a new string.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <body> <script> var x="Hello"; var y="World"; var z=x+y; document.write(z); </script> </body> </html> |
Here another example of conct(str)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <body> <script> var a = new String( "Hello" ); var b = new String( "Friends" ); var c = a.concat( b ); document.write("Concatenated String : " + c); </script> </body> </html> |
indexOf(str)
String indexOf(str) method returns the index position of the given string.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <script> var x="Tutorial provide by tutorialandexample"; var y=x.indexOf("by"); document.write(y); </script> </body> </html> |
lastindexOf(str)
String lastIndexOf(str) method returns the last index position of the given string.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <script> var x="Tutorial provide by tutorialandExamole"; var y=x.lastIndexOf("by"); document.write(n); </script> </body> </html> |
match()
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <body> <script> var str = "To more detail, see Chapter 2.5.4.1"; var re = /(chapter \d+(\.\d)*)/i; var find = str.match( re ); document.write(find ); </script> </body> </html> |
toLowerCase()
It returns the given string in lowercase letters.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <script> var x="HELLO WORLD"; var y=x.toLowerCase(); document.write(y); </script> </body> </html> |
toUpperCase()
It returns the given string in uppercase letters.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <script> var x="hello world"; var y=x.toUpperCase(); document.write(y); </script> </body> </html> |
valueOf()
It returns the primitive value of a string object.
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html> <body> <script> var str = new String("Hello world"); document.write(str.valueOf( )); </script> </body> </html> |