How to remove a specific character from a string and put it back later when needed Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to remove a specific character from a string and put it back later when needed without wasting too much if your time.

The question is published on by Tutorial Guruji team.

For example

let myString = "This is my string";

let replacedString = myString.replace(/ /g, "") //Thisismystring

Now that all the whitespaces have been removed, how do I put them back in the exact position?

Additionally, let’s suppose the replaced string undergoes some change and becomes

let myChangedString = "(T)(h)(i)(s)(i)(s)(m)(y)(s)(t)(r)(i)(n)(g)";

Now I want to put the whitespaces back where they used to be i.e after (s) and before (i), after (s) and before (m), after (y) before (s)

I’ve spent a couple of hours on this and been stuck in the same position, any form of help would be greatly appreciated.

EDIT: Solved, thank you very much.

Answer

Better you just transform your original array. Loop through array and modify the char is not empty.

let myString = "This is my string";

let chars = [...myString].map(item => item !== ' ' ? '(' + item + ')':  item)

console.log(chars.join(''))
We are here to answer your question about How to remove a specific character from a string and put it back later when needed - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji