有没有一种快速的方法或函数可以将子字符串插入到 Typescript 中的现有字符串中?

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

一定有一个简单的方法可以做到这一点,但我无法在 Typescript.

中弄清楚如何做到这一点

我有字符串:

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

我想以 result 字符串结束,其值为:

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

有没有办法轻松做到这一点? npm 库也很有用。 谢谢

在JavaScript、strings are immutable。但是,您可以这样做:

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"