c#中有没有去掉一个字符串中的指定的某些字符?

2024-11-15 11:11:00
推荐回答(1个)
回答1:

1、String.Replace:

str = str.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);

2、String.Aggregate:

string[] split = str.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.None); 

str = split.Aggregate((str1, str2) => str1 + str2);

3、Regex.Replace:

str = Regex.Replace(str, "[\r\n\t]", string.Empty, RegexOptions.Compiled);

4、Char Array:

string s = "abc";

int len = s.Length;

char[] s2 = new char[len];

int i2 = 0;

for (int i = 0; i < len; i++)

{

char c = s[i];

if (c != '\r' && c != '\n' && c != '\t')

s2[i2++] = c;

}

return new String(s2, 0, i2);

5、String.Concat:

String.Concat(str.Split(new char[] { '\t', '\r', '\n' },

StringSplitOptions.None));