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.
mnille
  • 1,328
  • 4
  • 16
  • 20
Suliman Sharif
  • 607
  • 1
  • 9
  • 26
  • `"ATOM LEU 0.1234 C 0.123 0.32 0.34".replace("C", "&")` – Rajaprabhu Aravindasamy Aug 08 '16 at 18:36
  • @RajaprabhuAravindasamy "The letter of C can essentially be anything." – yuriy636 Aug 08 '16 at 18:36
  • I want to replace by the location of C not the actual letter itself. – Suliman Sharif Aug 08 '16 at 18:37
  • 4
    Possible duplicate of [How do I replace a character at a particular index in JavaScript?](http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript) – Cameron Aug 08 '16 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? – Noah Herron Aug 08 '16 at 18:39
  • it is just a coincidence but the letter can be anything. I just put in a C because it is the most common as a carbon atom. The formatting of the string is exactly that. – Suliman Sharif Aug 08 '16 at 18:43
  • So if you could have a string with multiple substrings to replace. Do you want to replace all of them? – Noah Herron Aug 08 '16 at 19:03
  • Ya there are going to be multiple strings – Suliman Sharif Aug 08 '16 at 19:10
  • Also do you know anything about the string other than what substring you want to replace? Specifically do you know the location of the substring that you want to replace? Meaning you would know exactly where to replace. Even though you might know what you are replacing at that location. – Noah Herron Aug 08 '16 at 19:19

4 Answers4

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"
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • 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. – Noah Herron Aug 08 '16 at 18:51
  • 1
    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 – jmcgriz Aug 08 '16 at 18:57
  • @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". – Noah Herron Aug 08 '16 at 19:01
  • @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()`. – Noah Herron Aug 08 '16 at 19:10
  • @jmcgriz that worked out well thank you and is exactly what I wanted. – Suliman Sharif Aug 08 '16 at 19:20
0

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

Eduard Braun
  • 370
  • 3
  • 5
  • 1
    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 – empiric Aug 08 '16 at 18:48
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);
Abhay
  • 6,410
  • 4
  • 26
  • 34
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);
Noah Herron
  • 630
  • 4
  • 23