How do I replace a character at a particular index

2018-12-31 03:09发布

I have a string, let's say Hello world and I need to replace the char at index 3. How can I replace a char by specifying a index?

var str = "hello world";

I need something like

str.replaceAt(0,"h");

19条回答
泪湿衣
2楼-- · 2018-12-31 03:26

There is no replaceAt function in JavaScript. You can use the following code to replace any character in any string at specified position:

function Rep() {
    var str = 'Hello World';
    str = setCharAt(str,4,'a');
    alert(str);
}

function setCharAt(str,index,chr) {
    if(index > str.length-1) return str;
    return str.substr(0,index) + chr + str.substr(index+1);
}
<button onclick="Rep();">click</button>

查看更多
无色无味的生活
3楼-- · 2018-12-31 03:27

If you want to replace characters in string, you should create mutable strings. These are essentially character arrays. You could create a factory:

  function MutableString(str) {
    var result = str.split("");
    result.toString = function() {
      return this.join("");
    }
    return result;
  }

Then you can access the characters and the whole array converts to string when used as string:

  var x = MutableString("Hello");
  x[0] = "B"; // yes, we can alter the character
  x.push("!"); // good performance: no new string is created
  var y = "Hi, "+x; // converted to string: "Hi, Bello!"
查看更多
冷夜・残月
4楼-- · 2018-12-31 03:27

Here is a version I came up with if you want to style words or individual characters at their index.

replaceAt( yourString/orArrayOfStrings , yourArrayOfIndexes ) 

Working example: https://codesandbox.io/s/n9yl9q939j

function replaceAt(string, indexArray) {
  let newString = [...string];

  for (let i = 0; i < indexArray.length; i++) {
    newString = Object.assign(newString, {
      [indexArray[i]]: <b>{newString[indexArray[i]]}</b>
    });
  }

  return newString;
}

And here is another alternate method

function replaceAt(string, indexArray) {
  const newString = [...string];

  return newString.map(i => {
    indexArray.map(i => {
      return (newString[i] = <b>{newString[i]}</b>);
    });
    return i;
  });
}
查看更多
零度萤火
5楼-- · 2018-12-31 03:28

Work with vectors is usually most effective to contact String.

I suggest the following function:

String.prototype.replaceAt=function(index, char) {
    var a = this.split("");
    a[index] = char;
    return a.join("");
}

Run this snippet:

String.prototype.replaceAt=function(index, char) {
    var a = this.split("");
    a[index] = char;
    return a.join("");
}

var str = "hello world";
str = str.replaceAt(3, "#");

document.write(str);

查看更多
冷夜・残月
6楼-- · 2018-12-31 03:28

This works similar to Array.splice:

String.prototype.splice = function (i, j, str) {
    return this.substr(0, i) + str + this.substr(j, this.length);
};
查看更多
浪荡孟婆
7楼-- · 2018-12-31 03:32

You can't. Take the characters before and after the position and concat into a new string:

var s = "Hello world";
var index = 3;
s = s.substr(0, index) + 'x' + s.substr(index + 1);
查看更多
登录 后发表回答