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:
- String literal.
- Using new keyword.
String literal
By using double quotes string literal is created. Syntaxvar stringname="string value";
<!DOCTYPE html>
<html>
<body>
<script>
var str="Hello String Literal";
document.write(str);
</script>
</body>
</html>
Hello String Literal
Using new keyword
Syntaxvar stringname=new String("new keyword")
<!DOCTYPE html>
<html>
<body>
<script>
var stringname=new String("Hello String");
document.write(stringname);
</script>
</body>
</html>
Hello String
String properties
There are list of properties of string object and there description.- constructor.
- length.
- prototype.
string.constructor
string.length
object.prototype.name = value
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>
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>
<!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.<!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.<!DOCTYPE html>
<html>
<body>
<script>
var x="Tutorial provide by tutorialandExamole";
var y=x.lastIndexOf("by");
document.write(n);
</script>
</body>
</html>
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>
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>
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>
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>