How to get first character of string?

后端 未结 17 2545
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 09:43

I have a string, and I need to get its first character.

var x = \'somestring\';
alert(x[0]); //in ie7 returns undefined

How can I fix my co

17条回答
  •  被撕碎了的回忆
    2020-11-27 10:32

    x.substring(0,1)

    Details

    substring(start, end) extracts the characters from a string, between the 2 indices "start" and "end", not including "end" itself.

    Special notes

    • If "start" is greater than "end", this method will swap the two arguments, meaning str.substring(1, 4) == str.substring(4, 1).
    • If either "start" or "end" is less than 0, it is treated as if it were 0.

提交回复
热议问题