javaScript string objects


javaScript String Objects


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.charAtReturns a Character at Specified index
2.charCodeAtReturns a Character Code At a Specified index
3.concatConcatenation of more Than one String
4.indexOfReturn The First position of the Sub String
5.lastIndexOf Return The Last position of the Sub String
6.matchReturn The matched Array based on regular expression
7.replaceReplace the Array of String using the regular expression
8.searchSearch The Sub-String in the Given Specified String
9.splitSplit a String in many parts
10.sliceSlice The String into many sub-string according the passed argument
11.subStrReturns the substring of a given length from given index position
12.subStringReturns a substring from one index , up to another index but not including it
13.toLowerCaseConvert String in Lower Case String
14.toUpperCaseConvert 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.\nnew line
5.\rcarriage return
6.\ttab
7.\bbackspace
8.\fform 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