String is a very special built-in object because of the way you can create it.
All strings belong to the built-in String object
Var string = new string (‘Johny!”);
We can do the following string operations using javascript.
String.length:
var str1 = “abc”
var str2 = “a b c”
var str3 = “_”abc_”"
document.write(str1.length + “<BR>”)
document.write(str2.length + “<BR>”)
document.write(str3.length)
charAt()
This method returns the character whose index is equal to the argument of the method.
var name = “Peter”
document.write(name.charAt(1))
var str = “I am a string!”
for (var i = 0; i < str.length; ++i)
{
document.write(str.charAt(i))
}
Indexof()
Returns the index of first occurrence.
var str = “August 27, 1985″
document.write(str.indexOf(7))
lastIndexOf()
Returns the index of last Occurrence.
var str = “a/b/c”
document.write(str.lastIndexOf(“/”))
substring()
The substring() method returns a set of characters within its calling String object.
var str = “abcd”
var seg = str.substring(1, 3)
Hope this helps,
Happy Coding.
Comments are closed.