TypeScript中StringBuilder的等价物是什么? [重复]

问题描述 投票:3回答:1

这个问题在这里已有答案:

所以我有一个包含百分比符号%的字符串。我希望能够使用#将其替换为TypeScript符号。我还在学习TypeScript,所以它的语法仍然让我失望。我设法在C#中实现了我需要的解决方案,这段代码可以在下面找到:

 public static void Main(string[] args)
 {
     string data = "AHu%odk"; //The % character will always be at index 3
     string finalResult = "";
     if (data.Contains("%"))
     {
          StringBuilder sbFirstIndex = new StringBuilder(data);
          sbFirstIndex[3] = '#'; 
          finalResult = sbFirstIndex.ToString();
          Console.WriteLine("Appended string now looks like this: {0} \n", finalResult);
     }
     else
     {
           Console.WriteLine("False. It does not contain it");
     }
 }

这是我的TypeScript实现,但是如果检查字符的索引我就卡住了:

changePercantage(input: string): void {
    var data= "AHu%odk";
    var finalResult = ""; 
    var index = result.indexOf("%");

    if (index == 3) { 
        //Replace the percentage with the # character
    }
}

我使用的TypeScript版本是v2.3.3.0

Angular:v5.0.0

因为:くzxswい

Npm:v8.9.3

typescript angular5 typescript2.0 c#-6.0
1个回答
4
投票

在Typescript中,对于字符串替换,请使用RegExp

v5.6.0
© www.soinside.com 2019 - 2024. All rights reserved.