在第一个字符串后替换字符串中出现的所有字符

时间:2018-02-12 09:41:29

标签: c# string

我有一个像

这样的字符串
string s = "abc; abc bla bla ;;;;; bla bla";

我想用;替换第一个:以外的所有内容。我可以得到如下计数:

int t = s.Where(e => e.ToString() == ";").Count();

如果我s.Replace(';', ':'); ;所有:都替换为 int a, *p_a;//declaration of normal variable and int pointer variable a = 56; //simply assign value p_a = &a; //save address of "a" to pointer variable *p_a = 15; //override the value of the variable //print 0xfoo and 15 //- first is address, 2nd is value stored at this address (that is called dereference) printf("pointer p_a is having value %d and targeting at variable value %d", p_a, *p_a); 。有人可以告诉我如何实现这一目标。

1 个答案:

答案 0 :(得分:6)

有一点正则表达式:

string s = "abc; abc bla bla ;;;;; bla bla";
var regex = new Regex("(?<!^[^;]*);");
var result = regex.Replace(s,":");
Console.WriteLine(result);

实例:http://rextester.com/ORZU81353