为什么我不能在 javascript 字符串中交换字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25345108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:24:25  来源:igfitidea点击:

Why can't I swap characters in a javascript string?

javascriptarrays

提问by Vandana Bhat

I am trying to swap first and last characters of array.But javascript is not letting me swap. I don't want to use any built in function.

我正在尝试交换数组的第一个和最后一个字符。但是 javascript 不允许我交换。我不想使用任何内置函数。

function swap(arr, first, last){
    var temp = arr[first];    
    arr[first] = arr[last];
    arr[last] = temp;
}

回答by Oriol

Because strings are immutable.

因为字符串是不可变的。

The array notation is just that: a notation, a shortcut of charAtmethod. You can use it to get characters by position, but not to set them.

数组符号就是:一个符号,一个charAt方法的快捷方式。您可以使用它来按位置获取字符,但不能设置它们。

So if you want to change some characters, you must split the string into parts, and build the desired new string from them:

因此,如果要更改某些字符,则必须将字符串拆分为多个部分,并从中构建所需的新字符串:

function swapStr(str, first, last){
    return str.substr(0, first)
           + str[last]
           + str.substring(first+1, last)
           + str[first]
           + str.substr(last+1);
}

Alternatively, you can convert the string to an array:

或者,您可以将字符串转换为数组:

function swapStr(str, first, last){
    var arr = str.split('');
    swap(arr, first, last); // Your swap function
    return arr.join('');
}

回答by Dalorzo

Let me offer my side of what I understood: swapping items of an array could be something like:

让我谈谈我的理解:交换数组的项目可能是这样的:

var myFish = ["angel", "clown", "mandarin", "surgeon"];
var popped = myFish.pop();
myFish.unshift(popped) // results in ["surgeon", "angel", "clown", "mandarin"]

Regarding swaping first and last letters of an strings could be done using Regular Expressionusing something like:

关于交换字符串的第一个和最后一个字母可以使用正则表达式完成,例如:

"mandarin".replace(/^(\w)(.*)(\w)$/,"")// outputs nandarim ==> m is last character and n is first letter

回答by JohnHanr

function swapStr(str, first, last) {
if (first == last) {
  return str;
}

if (last < first) {
  var temp = last;
  last = first;
  first = temp;
}

if (first >= str.length) {
  return str;

}
return str.substring(0, first) +
  str[last] +

  str.substring(first + 1, last) +

  str[first] +
  str.substring(last + 1);

}

}

回答by Zexus

I just ran your code right out of Chrome, and it seemed to work find for me. Make sure the indices you pass in for "first" and "last" are correct (remember JavaScript is 0-index based). You might want to also try using console.log in order to print out certain variables and debug if it still doesn't work for you.

我刚从 Chrome 中运行你的代码,它似乎对我有用。确保为“first”和“last”传入的索引是正确的(请记住 JavaScript 是基于 0 索引的)。您可能还想尝试使用 console.log 来打印某些变量并在它仍然不适合您时进行调试。

EDIT: I didn't realize you were trying to manipulate a String; I thought you just meant an array of characters or values.

编辑:我没有意识到您正在尝试操作字符串;我以为你只是指一组字符或值。

My code

我的代码