Thursday, 2 June 2011

Complete string functions in JavaScript



String : In JavaScript  string is an object and it is providing number of properties and methods. String is collection of characters. Each character contains the index number and it starts from ‘0’ and ends with the total characters.
Sample Script :
<script>
str1=new String("Welcome to www.danphp.com website");
str="Welcome to PHP Tutorial Notes Site";
alert(str1)
alert(str)
</script>
Index of: By using this function we can find out the index number of input characters from a string. If the input character is not available it returns the -1.
Sample Script :
<script>
str="Welcome PHP Tutorial Site";
alert(str.indexOf('e',2))
</script>

If you executes the above script it returns the output 6. Why because in the index.Of function of e I have given a ,2. It means it searches for the second index e. The second index number of e is 6.
Length: By using this property we can get the total number of string characters.
Sample Script :
<script>
str="Welcome To JavaScript Tutorial Website";
alert(str.length)
</script>
If you execute the above script it returns the 38 output.
Sample Script :
<script>
str="Welcome To JavaScript Tutorial Website"
for(i=0;i<str.length;i++)
{
j=str.indexOf("e",i)
i=j;
if(j==-1)
break;
document.write(j+"<br>")
}
</script>
CharAt : This function returns the character of index number.
<script>
str="Welcome to PHP Tutorial Notes Website";
alert(str.charAt(6))
</script>
If you execute the above script it returns the 6 output.
charCodeAt: This function returns the Ascii value of a character from a string based on index number.
Ascii stands for American stranded code for information interchange.  Every character contains the ascii value. The range contains 0 to 265.
Ex : space ascii value is 32
A=65
a=97
0=48
Z=97
z=27
Sample Script :
<script>
str="Welcome to PHP Tutorial Website";
alert(str.charCodeAt(0))
</script>
If you execute the above script , you can see the output 87.
String.fromCharCode : It returns the character of ascii value.
Sample Script:
<script>
str="Welcome to PHP Tutorial WebSite";
alert(String.fromCharCode(122))
</script>
Match: By using this function we can compare two strings and it returns the matching values.
<script>
str="Welcome to PHP Tutorial WebSite";
alert(str.match("PHP"))
</script>
Replace : By using this function we can replace a string value with new value.
Sample Script :
<script>
str=”Welcome to PHP Tutorial WebSite”;
alert(str.replace(“PHP”,”JavaScript”))
</script>
ToLowercase : By using this function we can convert the input characters of a string into lower case to upper case.
Sample Script :
<script>
str="Welcome to PHP Tutorial WebSite";
alert(str.toLowerCase())
</script>
Substr : By using this function we can get the substring.
Substring: It is same as substr, but total of characters is the subtraction value of second argument of the first argument.
Math: Math is an object providing number of Mathematical functions.
Math.Random:  By using this function we can get the random numbers between the rang 0 and 1.
Sample Script :
<script>
document.write(Math.random(1))
</script>
Math.Round: It rounds the floating point number to the nearest integer value.
Sample Script:
<script>
document.write(Math.round(10.49))
</script>
Math.Max: By using this function we can get the maximum number from a group of numbers.
Sample Script :
<script>
document.write(Math.max(10,20,30))
</script>

Math.min : This function returns the minimum number from a group numbers.
Sample Script :
<script>
document.write(Math.min(10,20,30))
</script>
Math.floor:  By using this function we can get the nearest lowest integer value of a floating point number.
Sample Script :
<script>
document.write(Math.floor(10.99))
</script>
Math.cell: “This” is a current object. We can get the properties of current object using This.
Sample Script :
<script>
function fun1(t)
{
alert(t.id)
}
</script>
<input type="text" id="txt" onclick="fun1(this)">
<input type="button" value="Click" onclick="fun1(this)" id="but1">

Focus : By using this function we can focus an element.
Sample Script:

<script>
function fun1()
{
document.getElementByid('txt3').focus()
}
</script>
<input type="text" id="txt1">
<input type="text" id="txt3">
<input type="button" value="Click" onclick="fun1" id="but1">

Select : By using this function we can select the text of a control.
Sample Script :
<script>
function fun1()
{
document.getElementByid('txt3').select()
}
</script>
<input type="text" id="txt1">
<input type="text" id="txt3">
<input type="button" value="Click" onclick="fun1" id="but1">

0 comments:

Post a Comment