JavaScript String Objects
Javascript Contains a Large set of String Objects Which is used for
Manipulating the Characters or set of Characters. In JavaScript, You
Can define a string with or without use of var.
Ex :
var str="i-World-Tech";
str="i-World-Tech";
To define the String, you can Pass the String value to the String Class.
Ex :
var str= String("i-World-Tech");
var str=new String("i-World-Tech");
Properties of the String Objects
1. Length
it Returns the Length of the String.
Ex :
var str="Technology";
document.write(str.length);
2. Constructor
When we are declaring the String, same time Passed the Value to the String
Class, as i mentioned earlier.
Ex :
var str=new String("i-World-Tech");
Methods of the String Objects
Sr. | Methods | Function of the Method |
---|---|---|
1. | charAt | Returns a Character at Specified index |
2. | charCodeAt | Returns a Character Code At a Specified index |
3. | concat | Concatenation of more Than one String |
4. | indexOf | Return The First position of the Sub String |
5. | lastIndexOf | Return The Last position of the Sub String |
6. | match | Return The matched Array based on regular expression |
7. | replace | Replace the Array of String using the regular expression |
8. | search | Search The Sub-String in the Given Specified String |
9. | split | Split a String in many parts |
10. | slice | Slice The String into many sub-string according the passed argument |
11. | subStr | Returns the substring of a given length from given index position |
12. | subString | Returns a substring from one index , up to another index but not including it |
13. | toLowerCase | Convert String in Lower Case String |
14. | toUpperCase | Convert String in Upper Case String |
Special Characters
Special Characters are reserved set of the characters which is used to
Manipulate the String. List of the Special Characters are given in the
Following Tables.
Sr. | Special Characters | Meanings |
---|---|---|
1. | \' | single quote |
2. | \" | double quote |
3. | \\ | backslash |
4. | \n | new line |
5. | \r | carriage return |
6. | \t | tab |
7. | \b | backspace |
8. | \f | form feed |
Example of the String Objects
1. toUpperCase and toLowerCase
<html> <head> <title> String Examples </title> <script type="text/javascript"> function convert() { var str; str=document.getElementById("t1").value; document.getElementById("t1").value=str.toUpperCase(); } </script> </head> <body> <h1> i-World-Tech </h1> Name : <input type="text" id="t1" size="30"><br/> <input type="button" value="Upper Case" onclick="convert()"><br/> </body> </html> |
2. charAt
Ex :
var str="Information";
var str1=str.charAt(2);
document.write(str1);// "f"
3. concat
Ex :
var lan1="JAVA ";
var lan2="C++ ";
var lan3="javascript ";
var lan4="HTML ";
var lan=lan1.concat(lan2,lan3,lan4);
document.write(lan);
OUTPUT :
JAVA C++ javascript HTML
4. indexOf and lastIndexOf
Ex :
var str="java is a better programming language than C++";
var index=str.indexOf("better");//10
5. match
Ex :
var str="i-World-Tech is a Technical blog";
var exp=/Technical/gi;
var result=str.match(exp);
6. replace
Ex :
var str="i-World-Tech is a Technical blog";
var exp=/blog/gi;
var result=str.replace(exp,"website");
7. search
it will Search the Sub-string in the Specified String. it Will Return the Index
Position if Match is Found other return -1.
Ex :
var str="i-World-Tech is a Technical blog";
var exp=/Technical/gi;
var output=str.search(exp);
8. split
Ex :
var fb="Facebook is the Best Social Networking Site of the World";
var res=fb.split(" ");
document.write(res);
Output :
Facebook,is,the,Best,Social,Networking,Site,of,the,World
9. substr
it Will Returns the sub string of the Specified length start from the
Specified Index
Ex :
var eng="we are an Engineer";
var eng1=eng.substr(3,6); // are an
10. subString
it will Returns substring From one index to another Specified Index but
not including it.
Ex :
var str="hiii What are you doing now";
var str1=str.subString(5,10);
Thank You To All My Reader
Deepak Gupta
www.i-world-tech.blogspot.in
Related Post
1. Mini Project in C : Book Shop Inventory System
2. Interesting C++ Program
3. JavaScript Form Validation
4. HTML Marquee Tag
5. How To Add Music File To Website
6. How To Add Meta Tags To Blogger
7. In Visual Basic : How we Convert our statement in Computer Voice
8. JavaScript Math Object
9. How We Enable JavaScript in Browser
10. Batch Programming :How To make a Simple Calculator in Batch programming
Comments
Post a Comment