如何替换第一次出现的字符串中的字符串?

例如C:\\Test-Processed\1-Processed\2-Processed应该输出
C:\\Test-Processed\1\2

最佳答案

如下所示:

string originalStr = "C:\\Test-Processed\\1-Processed\\2-Processed";
string temp = "-Processed";
string str = originalStr.Substring(0, originalStr.IndexOf(temp) + temp.Length);
originalStr = str + originalStr.Substring(str.Length).Replace(temp, "");

originalStr将为:
originalStr = "C:\\Test-Processed\\1\\2"

09-25 17:15