如果字符放在字符串(数组)中,则将字符串值分配给字符

时间:2013-12-25 18:49:42

标签: javascript

我有:

var text = [];

text[0] = 'xyй';

text[0][0] = 'c';

alert(text[0][0]); // = 'xyй'

我认为有必要将字符串转换为数组吗?这是真的吗?

1 个答案:

答案 0 :(得分:0)

以下两种方式: Live demo (click).

var text = [];
text[0] = 'xyй';
//break each string at each character into array
text[0] = text[0].split('');
//replace array item
text[0][0] = 'c';
//rejoin array into string
text[0] = text[0].join('');
console.log(text[0]);

var text = [];
text[0] = 'xyй';
//replace first character (character 0) with 'c'
text[0] = text[0].replace(text[0].charAt(0), 'c');
console.log(text[0]);