2

suppose that i have

var name = "kanishka";

How can I cut only the last char so that the output will be 'a' instead?

i read the question

How can I cut the 1st char from a string in jquery?

by jin Yong ,

in this problem i know the length of the string . can u tell me how i can do this with out knowing the string length .

couldn't get help from the answers , please help

6 Answers 6

5

Using plain javascript, you can do

name.charAt(name.length-1)

More information on charAt http://www.w3schools.com/jsref/jsref_charat.asp

3
  • Finally someone using their brain and not substring (which is pointless and stupid for this case)! +1. of course, given that a string is an array, you can even do name[name.length-1].
    – OJ.
    May 10, 2011 at 4:09
  • @OJ: Why do you think substring() is worse than charAt() here, apart from being a few characters longer? By the way, accesing characters in a string via numeric property names is non-standard prior to ECMAScript 5 and is not supported in IE 7 or earlier (not sure about IE 8).
    – Tim Down
    May 10, 2011 at 9:00
  • @Tim: [] is supported in IE8. Not, as you said, in IE7 or earlier. @OJ: Strings are not arrays. String supports [] on modern browsers, but it's still not an array. May 11, 2011 at 2:12
4
Array.prototype.pop.apply(name);

^_^

or if you want to be boring:

name.substr(name.length-1);

or:

name.split('').reverse().shift()

or:

name.split(name.substr(0, name.length-1)).pop()

or:

name.constructor.prototype.charAt.call(name.split('').reverse(), 0)

or:

var chr; name.split('').forEach(function(c){ chr=c; });

or (okay, these are serious):

/.$/.exec(name)[0]

or

name[name.length-1];
4
  • They're all boring given that to pull out a single char you can index directly into the array and avoid all of the overhead that you've suggested.
    – OJ.
    May 10, 2011 at 4:10
  • @OJ - what, don't like my unnecessary overhead?!? :(
    – user578895
    May 10, 2011 at 4:13
  • @OJ: You can only use [] if you don't care about IE6 and IE7 support. (IE6 + IE7 make up 10-20% of the browser users out there, a figure that is -- thankfully! -- rapidly falling, but still significant). May 11, 2011 at 2:03
  • @TJ Yes mate, you're right. Those browsers are a PITA. I personally refuse to support browsers that old (2 major version numbers behind is a joke, and IE10 isn't far off). Let's make it our duty to get people to update :)
    – OJ.
    May 11, 2011 at 23:37
2

If you want the "a" in "kanishka":

var name = "kanishka";
name = name.substring(name.length - 1);

Gratuitous live example

4
  • what's with all the -2 in the answers? That'll give you 'ka'
    – user578895
    May 10, 2011 at 3:57
  • @cwolves: It was a typo, apparently you saw it before I fixed it. :-) May 10, 2011 at 3:57
  • 1
    clicks the button aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    – Nic
    May 10, 2011 at 3:58
  • name.slice(-1) is a little neater.
    – Tim Down
    May 10, 2011 at 23:40
1
name = name.substr(name.length-1,name.length);
2
  • he wants the last char, not the string up to the last one
    – user578895
    May 10, 2011 at 3:55
  • try again! :) What you want: name.substr(name.length-1)
    – user578895
    May 10, 2011 at 3:55
1

A roundup of your options as presented by the various answers here:
(note that this is a CW answer, as it represents community content)

  1. substring:

    var lastChar = name.substring(name.length - 1);
    
    • Verbose, but clear.
    • Broadly-supported.

  2. substr:

    var lastChar = name.substr(name.length - 1);
    
    • Verbose (thought a tiny bit less so), but clear.
    • Broadly-supported (though not strictly standard, and there are edge cases with some browsers; see comments).

  3. slice:

    var lastChar = name.slice(-1);
    
    • Concise, clear once you're used to the idiom, but unclear to non-l33t c0d3rz.
    • Broadly-supported.
    • Tends to be slower or slowest on most browsers (but not all).

  4. charAt:

    var lastChar = name.charAt(name.length - 1);
    
    • Verbose, but very clear.
    • Broadly-supported.
    • Fastest (or tied for fastest with []) on most browsers (usually by a fair margin).

  5. []:

    var lastChar = name[name.length - 1];
    
    • Concise and clear.
    • Not supported by IE7 and earlier.
    • Fastest (or tied for fastest with charAt) on most browsers (usually by a fair margin).

(Due respect to cwolves' flights of outre approaches, I stuck to the mainstream like the boring plodder I am.)

Gratuitous jsperf test

Which should you use? It's totally up to you. Note that all of the options above are plenty fast, the odds that the speed of this operation are actually important in 99.999% of real-world cases are very low indeed.

3
  • 1
    substr() isn't strictly standard, although as you say, it is broadly supported. Its implementation is slightly different in IE 7 (again, not sure about 8 or 9) from other browsers in that a negative character index is treated as 0 rather than being subtracted from the length.
    – Tim Down
    May 11, 2011 at 8:40
  • @TIm: Hey, look at that. It's not in the main spec, but in an appendix on it (B.2.3, to be precise, in both 3rd and 5th editions). No wonder I never use substr. :-) May 11, 2011 at 8:43
  • J.: slice() is where it's at for substrings in JS. It's standard, has negative offsets for teh l33t c0d3rz and is universally supported.
    – Tim Down
    May 11, 2011 at 8:46
0

A neat, short, cross-browser way is

name.slice(-1);
1
  • Nice. :-) Curiously, this tends to be a slow option on several browsers (not that it matters), and I can't think why. See the CW post rounding up the options. May 11, 2011 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.