Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I delete one part of the string without deleting the same set of characters in another place?

I’m trying to clean up a string (URL). I need that every time it contains ipfs://ipfs/ to replace it by a single ipfs/.
The full string is ipfs://ipfs/QmR6xjBCn
but it should be ipfs://QmR6xjBCn
In some cases I get string that are already clean, in other cases I dont’.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use the replace() function. If the text is not in the string then it won’t replace anything. So it only works when the double ipfs is there.

let str;
// example one
str = "ipfs://ipfs/QmR6xjBCn";
str = str.replace("ipfs://ipfs/","ipfs://");
console.log(str);

// example two, same code different url
str = "ipfs://SomethingRandom";
str = str.replace("ipfs://ipfs/","ipfs://");
console.log(str);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading