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

2024-12-16 17:40:22
推荐回答(2个)
回答1:

string s="淘宝梦琪动漫屋";
s = s.Replace("淘宝","");
意思把淘宝两个字替换成空,相当于去掉了

回答2:

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));