我有这样一个字符串:
this is my text more text texttext and here comes something special [S p e c i a l string] more random text
我想做的是去掉括号外面的空格,所以最后的结果是:
thisismytextmoretexttexttextandherecomessomethingspecial[S p e c i a l string]morerandomtext
现在我正在删除刚刚做的空格
myString.Replace(" ", String.Empty);
但是,有没有办法只选择需要的部分呢?
请您参考如下方法:
你可以使用 Regex :
var myText = @"this is my text more text texttext and here comes something special [S p e c i a l string] more random text";
var pattern = @"\s+(?![^\[]*\])";
var output = new Regex(pattern).Replace(myText, "");
输出将保持:
thisismytextmoretexttexttextandherecomessomethingspecial[S p e c i a l string]morerandomtext
