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

Is there a fast way or function to insert a substring into existing string in Typescript?

There must be an easy way to do this but I can’t figure out how to do so in Typescript.

I have the strings:

string origin = 'app-home';
string modifier = 'ua-';

And I want to end up with a result string with the value:

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

console.log(result); // app-ua-home

Is there a way to do this easily? npm libraries could also be useful.
Thanks.

>Solution :

In JavaScript, strings are immutable. However, you could do something like this:

TS Playground

const delimiter = '-';
const initial = 'app-home';
const insertValue = 'ua';

let result = '';

for (const unit of initial) {
  if (unit !== delimiter) result += unit;
  else result += `${delimiter}${insertValue}${delimiter}`;
}

console.log(result); // "app-ua-home"
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