Javascript String

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript:

  1. String literal.
  2. Using new keyword.

String literal

By using double quotes string literal is created. Syntax
var stringname="string value";
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str="Hello String Literal";  
document.write(str);   
</script>  
</body>  
</html>
Try Now Output
Hello String Literal

Using new keyword

Syntax
var stringname=new String("new keyword")
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var stringname=new String("Hello String");  
document.write(stringname);  
</script>  
</body>  
</html>
Try Now Output
Hello String

String properties

There are list of properties of string object and there description.
  1. constructor.
  2. length.
  3. prototype.
Constructor- It returns a reference to the string function that created the object. Syntax
string.constructor
Length-It returns the number of characters in a string. Syntax
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
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.
<!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>
Try Now

concat(str)

This method add two or more string and return a new string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Hello";    
var y="World";    
var z=x+y;    
document.write(z);    
</script>    
</body>  
</html>
Try Now Here another example of conct(str)
<!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>
Try Now

indexOf(str)

String indexOf(str) method returns the index position of the given string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Tutorial provide by tutorialandexample";    
var y=x.indexOf("by");    
document.write(y);    
</script>    
</body>  
</html>
Try Now

lastindexOf(str)

String lastIndexOf(str) method returns the last index position of the given string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Tutorial provide by tutorialandExamole";    
var y=x.lastIndexOf("by");    
document.write(n);    
</script>    
</body>  
</html>
Try Now

match()

<!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>
Try Now

toLowerCase()

It returns the given string in lowercase letters.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="HELLO WORLD";    
var y=x.toLowerCase();    
document.write(y);    
</script>    
</body>  
</html>
Try Now

toUpperCase()

It returns the given string in uppercase letters.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="hello world";    
var y=x.toUpperCase();    
document.write(y);    
</script>    
</body>  
</html>
Try Now

valueOf()

It returns the primitive value of a string object.
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str = new String("Hello world");  
document.write(str.valueOf( ));  
</script>  
</body>  
</html>
Try Now