8

Is it possible to replace the a character at a particular position with a string

Let us say there is say a string : "I am a man"

I want to replace character at 7 with the string "wom" (regardless of what the original character was).

The final result should be : "I am a woman"

Alnitak
  • 334,560
  • 70
  • 407
  • 495
Bobby Francis Joseph
  • 606
  • 2
  • 14
  • 34
  • 1
    See also here http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript (you can use the function of the selected answer, works as well for strings). – User May 28 '12 at 12:30
  • @lxx no, that function is no good as it replaces as many characters in the source string as were supplied - the OP here only wants _one_ character replaced. – Alnitak May 28 '12 at 12:45
  • Does this answer your question? [How do I replace a character at a particular index in JavaScript?](https://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript) – wesinat0r Dec 31 '19 at 04:34

3 Answers3

22

Strings are immutable in Javascript - you can't modify them "in place".

You'll need to cut the original string up, and return a new string made out of all of the pieces:

// replace the 'n'th character of 's' with 't'
function replaceAt(s, n, t) {
    return s.substring(0, n) + t + s.substring(n + 1);
}

NB: I didn't add this to String.prototype because on some browsers performance is very bad if you add functions to the prototype of built-in types.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • They really are immutable, but that doesn't mean you have to do index math. There is already a replace method. See my reply. – Slavo May 28 '12 at 12:31
  • @Slavo `.replace` doesn't support position based indices, unless you construct a needlessly complicated regexp. – Alnitak May 28 '12 at 12:32
  • I agree. The examples given in the question don't need complex RegEx. I guess it all depends on the real use case. – Slavo May 28 '12 at 12:34
  • @Slavo the OP's use case is replacing a character _by index_ - `.replace()` replaces _by content_. i.e. he wants to replace at the 7th position _regardless of what character is already there_. – Alnitak May 28 '12 at 12:34
  • I understand. What I'm saying is that his first example can be done by `str.replace("man", "woman");` – Slavo May 28 '12 at 12:37
  • @Slavo _no, it can't_ because the whole point is that he doesn't know a priori that the original content is "man" ! The relevant information is the _character index_, not its _value_! – Alnitak May 28 '12 at 12:40
  • @Alnitak. It seems to replace two characters. Otherwise its fine. How can that be solved. – Bobby Francis Joseph May 28 '12 at 13:08
  • @BobbyFrancisJoseph I had an unnecessary `-1` in the first `substring()` - should be fine now! I forgot that substring is _exclusive_ on the second parameter. – Alnitak May 28 '12 at 13:11
1

Or you could do it this way, using array functions.

var a='I am a man'.split('');
a.splice.apply(a,[7,1].concat('wom'.split('')));
console.log(a.join(''));//<-- I am a woman
Tom
  • 7,994
  • 8
  • 45
  • 62
-1

There is a string.replace() method in Javascript: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

P.S.
By the way, in your first example, the index of the "m" you are talking about is 7. Javascript uses 0-based indices.

Slavo
  • 15,255
  • 11
  • 47
  • 60
  • 4
    This doesn't satisfy the OP's problem. They want to access by index and string.replace() replaces with a regular expression. This is completely different. – Jeff LaFay Jun 14 '13 at 13:48