Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中将字符转换为字符串?_Javascript - Fatal编程技术网

如何在JavaScript中将字符转换为字符串?

如何在JavaScript中将字符转换为字符串?,javascript,Javascript,我正在使用JavaScript中的split“”方法将单词拆分为空格。 例如: 我有如下文字: var str ="Hello this is testing" 我打电话之后 str.split(' ') 现在我将得到Hello,这是tesing作为输出,当我这样做时 str[2] 我得到了l,但我想根据数组索引得到测试字。如何将str转换为数组,以便 str[2] //It should be testing. 字符串是不可变的,返回数组。必须将返回值赋给变量。例如: > var

我正在使用JavaScript中的split“”方法将单词拆分为空格。 例如:

我有如下文字:

var str ="Hello this is testing"
我打电话之后

str.split(' ')
现在我将得到Hello,这是tesing作为输出,当我这样做时

str[2]
我得到了l,但我想根据数组索引得到测试字。如何将str转换为数组,以便

str[2] //It should be testing.
字符串是不可变的,返回数组。必须将返回值赋给变量。例如:

> var str ="Hello this is testing";
  undefined
> str = str.split(' ');
  ["Hello", "this", "is", "testing"]
> str[3]
  "testing"
字符串是不可变的,返回数组。必须将返回值赋给变量。例如:

> var str ="Hello this is testing";
  undefined
> str = str.split(' ');
  ["Hello", "this", "is", "testing"]
> str[3]
  "testing"

当您拆分时,它实际上返回一个值

var foo = str.split(' ');
foo[2] == 'is' // true
foo[3] == 'testing' // true
str[2] == 'l' // because this is the old str which never got changed.

当您拆分时,它实际上返回一个值

var foo = str.split(' ');
foo[2] == 'is' // true
foo[3] == 'testing' // true
str[2] == 'l' // because this is the old str which never got changed.
写入str.split不会修改str。 相反,它返回一个包含单词的新数组

你可以写

var words = str.split(" ");
var aWord = words[1];
写入str.split不会修改str。 相反,它返回一个包含单词的新数组

你可以写

var words = str.split(" ");
var aWord = words[1];
返回数组时,它不会更改现有变量的值

例如

var str = "Hello this is testing";

var str_array = str.split(' ');

document.write(str_array[3]); //will give you your expected result.
返回数组时,它不会更改现有变量的值

例如

var str = "Hello this is testing";

var str_array = str.split(' ');

document.write(str_array[3]); //will give you your expected result.

我制作了这个html页面,它报告了以下内容:

function getStrs()
{
   var str = "Hello this is testing";
   var array = str.split(' ');
   for(var i=0; i < 4; i ++)
      alert(array[i]);
}

它报告你好。。。这是测试

我制作了这个html页面,它报告了以下内容:

function getStrs()
{
   var str = "Hello this is testing";
   var array = str.split(' ');
   for(var i=0; i < 4; i ++)
      alert(array[i]);
}

它报告你好。。。这是测试

aravinth str[2]应该是你希望达到的目标,Avinth str[2]应该是你希望达到的目标,除了foo[2]实际上等于is,str[2]实际上等于‘l’,欢迎你。请不要忘记选择一个正确的答案。除了foo[2]实际上等于is。str[2]实际上等于'l',不客气。请不要忘记选择正确的答案。