开发者

Javascript strings - getting the char at a certain point

开发者 https://www.devze.com 2023-01-15 13:00 出处:网络
I have a variable: var text = \"hello\"; 开发者_Go百科 I want to get the 0 positioned character, so:

I have a variable:

var text = "hello";
开发者_Go百科

I want to get the 0 positioned character, so:

var firstChar = text[0];

Simple. In firefox and chrome this works. In IE however i always get back 'undefined'

Any ideas why this might be happening in IE?


Strings aren't accessible like arrays in IE (prior to IE9). Instead you can use charAt, which is available cross-browser:

var text = "hello";
var firstChar = text.charAt(0);
// firstChar will be 'h'


You can use .substr().

var firstChar = text.substr(0,1);


I'm not sure why that doesn't work, but you could try using substr()

0

精彩评论

暂无评论...
验证码 换一张
取 消