Sponsored Ad

Wednesday, April 21, 2010

Substring Operations Using JavaScript

 

Substring operations are very common and useful for string manipulations. JavaScript also supports this operation by two methods:

substring()

substr()

Lets discuss both of them.

substr(5,8) function start counting string from 0 and find the 5th count of string character and calculate till length of 8 characters.

0-T 1-h 2-i 3-s 4-  5-S 6-u 7-b 8-s 9-t 10-r 11-i 12-n 13-g manipulation is brought you by CsharpTalk.com

as given in above example the substring result start with 5-S and end at 5+8 length=12-n.

 

Now lets discuss substring(5,8)

substring(5,8) function starts with 5th character and end before 8th character

0-T 1-h 2-i 3-s 4-  5-S 6-u 7-b 8-s 9-t 10-r 11-i 12-n 13-g manipulation is brought you by CsharpTalk.com

So as in above example the result of this operation will be 5-S to 7th character.

 

<html>
<body>
<script type="text/javascript">
var str= "This Substring manipulation is brought you by CsharpTalk.com"
document.write(str.substr(5,8))
document.write("<br /><br />")
document.write(str.substring(5,8))
</script>
<p>
Example of substr() and substring() of JavaScript.
</p>
</body>
</html>

 

Output of given program:

Output of str.substr(5,8)

Substrin

Output of str.substring(5,8)

Sub

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers