String Pattern搜索并替换为新的字符串模式

时间:2014-11-08 10:57:27

标签: c#-4.0

如何使用C sharp替换长文本中第一次出现的所有出现的字符串模式。请帮我一个例子

1 个答案:

答案 0 :(得分:0)

举个例子。请先尝试在互联网上搜索解决方案。如果找不到直接解决方案,请在此论坛中提问。

不是C#的确切代码,但将其视为伪代码。有很多方法可以做到。

// First method
int first_index_of = s.find(input_String);
String temp = input_String.substr(first_index_of, input_String.length);
temp = temp.replace("Old", "New");
input_String = input_String.substring(0,first_index_of) + temp;

//第二种方法

int i = 0;
while ( didn't reach end of string ){
 find next occurrence starting from index i

 // Ignore the first occurrence and Replace all other occurrences
 if ( Ctr >1) 
    replace the string

 // Exit if no more sub strings found
}

这些只是伪代码。我希望你在使用它之前做一些工作。