Forum Moderators: open

Message Too Old, No Replies

i need help

JavaScript cooding

         

sawkat

5:02 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



i want to take last 3 letters from the name.
so what i have to use - indexOf() or lastindexOf()

thanks for help

Fotiman

5:58 pm on Apr 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you have a string, and you want only a certain subset of characters, you could use the substr method:

[quote][pre]


var s = "sawkat";
var start = s.length - 3;
var lastThree = s.substr(start);
[/quote][/pre]

[edited by: Fotiman at 5:59 pm (utc) on April 1, 2009]

birdbrain

6:16 pm on Apr 1, 2009 (gmt 0)



Hi there sawkat,

our friend Fotiman has given you the last three string letters, so here is the string less the last three letters...


<script type="text/javascript">

var s="sawkat";
var start=s.length - 3;

alert(s.substring(0,start));

</script>


...to complete the collection. ;)

birdbrain

astupidname

7:18 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



Yet another way to get the last 3 characters from a string - use regEx:
var s="sawkat";
var s2 = s.match(/.{3}$/); //gets last 3 of any characters from string
alert(s2); //'kat'

astupidname

7:23 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



Or if you want to remove the last 3 characters:
var s = "sawkat";
var s3 = s.replace(/.{3}$/,''); //replace the last 3 of any characters with a blank string ''
alert(s3); //'saw'

sawkat

8:04 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



owow thanks my friend