Sunday, December 13, 2009

String functions in JavaScript


Most common string functions in JavaScript




Sometimes you need to deal with strings in javascript. It will be getting a length of a string or determine the character position in a string.




How to get a length of a string in java script?


Use the length method to find out how many characters in string. It returns the total characters in the string.

How Change the case of a string?


You can change the case of a string to lowercase or to upper case using javascript function.
To change a string to lowercase use toLowerCase() function and make uppercase use toUpperCase() function.

How to get the index of a character?


You can get the index of the first occurrence of a specified character using indexOf() function.
This returns -1 if the character not present in the given string, otherwise it returns the index of the first occurrence. The index starts from zero. There is an another method called lastIndexOf() to get the index of a specified character. It start checking right to left.

How create sub string in javascript?


Use substr() function to make sub strings in javascript. This method returns specified parts of the string. You have to
specify the starting position of the string and then how many characters do you want to extract from the string. Take into mind first character is 0. If ending position is greater than the total length of the string it returns the whole input string as result. Use substring() function to get all characters between starting and ending positions.





Demo- String functions in JavaScript.
1. Get string length using JavaScript.
Enter string to get length


view code

2.Change text case using JavaScript.
Enter string to change the case



view code




3.Get the index of a character using JavaScript.




 Enter a character



Type your text


view code

4.Make sub strings using JavaScript.

Enter a string to make sub string



Starting position



 Enter ending position


view code



Bookmark and Share




1. Example code for get string length using JavaScript.

JavaScript Code
<script type="text/javascript" language="javascript">

<!--

function getStringLength(str){

if(str==''){

alert("Please enter a string");

}else{

alert(str.length+" characters in the string");

}

return true;

}

-->

</script>
Html Code


<html>

 <head>

  <title>Get string length in JavaScript </title>

 </head>

<body>

 <input type="text" id="stringLength" name="stringLength" />
<input name="getStringLength" type="button" onClick="javascript:getStringLength(this.value);" value="Get Length" />

</body>

</html>

2. Sample code for change text case in JavaScript.

JavaScript code
<script type="text/javascript" language="javascript">

<!--

function changeCase(textCase){

var text=document.getElementById("txtUpperorLower").value;

if(text==''){

alert("Please enter a string");

return false;

}

switch(textCase){

case 1:{

alert(text.toUpperCase());

}break;

case 2:{

alert(text.toLowerCase());

}break;

}

return true;

}
-->

</script>

<html>

 <head>

  <title>Chanage text case in JavaScript </title>

 </head>

<body>

 <input type="text" name="txtUpperorLower" id="txtUpperorLower" value="" />

<input name="setUpperCase" id="setUpperCase" type="button" onClick="javascript:changeCase(1);" value="To Upper" /> <input name="setLowerCase" id="setLowerCase" type="button" onClick="javascript:changeCase(2);" value="To Lower" />

</body>

</html>

3. Sample code for get character index in JavaScript.

JavaScript code
<script type="text/javascript" language="javascript">

<!--

function getIndex(){

var text=document.getElementById("txtGetIndex").value;

var textChar=document.getElementById("txtCharacter").value;

if(text==''){

alert("Please enter a string");

return false;

}else if(textChar==''){

alert("Please enter a character");

return false;

}else if(textChar.length>1){

alert("Please enter a single character");

return false;

}

var index=text.indexOf(textChar)

if(index<0){

alert("Character not found");

}else{

alert("Index of "+textChar+" is "+index);

}

return true;

} -->


</script><html>

 <head>

  <title>Get character index in JavaScript </title>

 </head>

<body>

 Enter a character <input type="text" name="txtCharacter" id="txtCharacter" value="" size="5" /> Type your text

<input type="text" name="txtGetIndex" id="txtGetIndex" value="" />

<input name="setCharacterIndex" id="setCharacterIndex" type="button" onClick="javascript:getIndex();" value="Get Index" />

</body>

</html>

4. Sample code for making sub string using JavaScript.

JavaScript code
<script type="text/javascript" language="javascript">

<!--

function getSubstring(){

var start=document.getElementById("txtStart").value;

var end =document.getElementById("txtEnd").value;

var text=document.getElementById("txtToSubstring").value;

if(isNaN(start)){

alert("Please enter a valid start value");

return false;

}else if(isNaN(end)){

alert("Please enter a valid ending value");

return false;

}else if(start<0 || end<0 || end==0){

alert("Please enter a valid start and ending value");

return false;

}else if(text==''){

alert("Please enter a string to make substring");

return false;

}



alert(text.substr(start,end));

return true;

}-->


</script><html>

 <head>

  <title>Make substring in JavaScript </title>

 </head>

<body>

Enter a string to make substring <input type="text" name="txtToSubstring" id="txtToSubstring" value="Hello World" /><br />

Starting position <input type="text" name="txtStart" id="txtStart" value="0" size="5" /><br />

Enter ending position <input type="text" name="txtEnd" id="txtEnd" value="2" size="5" />

<input name="getSubstring" id="getSubstring" type="button" onClick="javascript:getSubstring();" value="Make Substring" />

</body>

</html>


 »   How to get screen resolution in JavaScript
 »   How to limit characters in textarea using JavaScript
 »   How to validate decimal number in JavaScript
 »   How to validate an email address in JavaScript
 »   How to validate date using JavaScript
 »   JavaScript String functions
 »   How to validate multiple select list box in JavaScript
 »   How to generate random numbers in JavaScript
 »   How to validate multiple check box in JavaScript
 »   How to validate user login in JavaScript
 »   How to validate drop down list in JavaScript
 »   How to validate radio button group in JavaScript
 »   How to create JavaScript alerts
 »    How to create popup windows in JavaScript
 »   How to count words in a text area using JavaScript

©-Copyright By Duminda Chamara
JavaScript Validation  

1 comments:

Unknown said...

trước khi mang bầu bà bầu nên tìm hiểu những dieu can biet khi mang bau của chúng tôi, Thuốc bổ xương khớp Glucosamine của Mỹ hàng chĩnh hãng được nhập khẩu từ Mỹ giúp chị em bổ sung can-xi cho xương khớp, bà bầu uống tinh dầu tỏi có được không là điều mà nhiều chị em thắc mắc đấy nhỉ, bà bầu ăn tỏi có tác dụng gì có lợi không hay hại cho cả mẹ và bé thế, tác dụng của mít đối với bà bầu khi mang thai như thế nào, lợi ích của sữa chua đối với bà bầu là điều mà nhiều người thắc mắc, sữa chua rất tốt với bà bầu cũng như là bé trong bụng, tác dụng của nha đam đối với bà bầu là rất có hại cho cơ thể mẹ và bé vì nha đam tiết ra chất nhờn rất độc hại, bà bầu ăn nhãn có được không vì nhãn là một loại thực phẩm được nhiều người ưa thích trong các dịp đi chơi, bà bầu ăn cà tím có được không nhỉ, cà tím nướng thì hết sảy đó.