0

I have a string that looks like this

ATOM LEU 0.1234 C 0.123 0.32 0.34

How do I replace the C with .replace() to & in Javascript by getting only the location of C? The letter of C can essentially be anything.

I want

ATOM LEU 0.1234 & 0.123 0.32 0.34.
9
  • "ATOM LEU 0.1234 C 0.123 0.32 0.34".replace("C", "&") Commented Aug 8, 2016 at 18:36
  • @RajaprabhuAravindasamy "The letter of C can essentially be anything." Commented Aug 8, 2016 at 18:36
  • I want to replace by the location of C not the actual letter itself. Commented Aug 8, 2016 at 18:37
  • 4
    Possible duplicate of How do I replace a character at a particular index in JavaScript? Commented Aug 8, 2016 at 18:38
  • The character that you are looking for/replacing is it unique to the string it in? Is it a coincidence or a standard that there is a single "C" in that string? Commented Aug 8, 2016 at 18:39

4 Answers 4

4

You can do it by using substring(),

var str = "ATOM LEU 0.1234 C 0.123 0.32 0.34";
var res = str.substring(0, 16) + "&" + str.substring(17);
console.log(str); //"ATOM LEU 0.1234 & 0.123 0.32 0.34"
Sign up to request clarification or add additional context in comments.

5 Comments

It might be better to find the location of the string you want to replace rather than using a static index of 16 (you might not know were the substring you want to replace is). Good solution tho.
I'd suggest to update this and use var res = [str.substring(0, 16), str.substring(17)].join("&"). It's a subtle change, but it will run a lot faster if you need to process a lot of these at once. Also in your example, it should say console.log(res), right now it's just logging the original string
@empiric It would replace the first instance of the non unique character. Stating "The letter of C can essentially be anything." doesn't mean that it will not be unique to the string; just that it does not have to be "C".
@empiric where did the second A come from? Also he wants to do it based off of location ("by getting only the location of C"). Which makes me think that the OP is not sure where in the string the substring they are trying to replace is. If you would like to replace all of them just call it in a loop until the substring is not longer present. If you don't need to know the location then just use the vanilla JavaScript replace().
@jmcgriz that worked out well thank you and is exactly what I wanted.
0

You can also try this :

var str = "ATOM LEU 0.1234 C 0.123 0.32 0.34";
var strExp = str.split(" ");
var newStr = str.replace(strExp[3], "&");
console.log(newStr);

2 Comments

How does he know to select the 4th element in the array of strings?
Bcz, i have assumed, this pattern will not be changed
0

Find the location of the string you want to replace. Then replace it with what you want. I wrote this not knowing how long the string you want to replace will be.

var str = "ATOM LEU 0.1234 C 0.123 0.32 0.34";
var strToReplace = "C";
var indexOfStrToReplace = str.indexOf(strToReplace);
var result = str.substring(0,indexOfStrToReplace) + "&" + str.substing(indexOfC + strToReplace.length);

Comments

0

var str = "ATOM LEU 0.1234 C 0.123 0.32 0.34";
var newStr = str.replace(str.charAt(16), "&");

1 Comment

Also be adviced if the char at position 16 is not unique in the string, every char will be replaced no matter what position it is in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.