C#VI:删除字符串中指定字符的几种方法
1个回答
展开全部
方法一,String.Replace
str = str.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);1
方法二, String.Aggregate
string[] split = str.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.None);
str = split.Aggregate<string>((str1, str2) => str1 + str2);12
方法三,Regex.Replace
str = Regex.Replace(str, "[\r\n\t]", string.Empty, RegexOptions.Compiled);1
方法四,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);
str = str.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);1
方法二, String.Aggregate
string[] split = str.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.None);
str = split.Aggregate<string>((str1, str2) => str1 + str2);12
方法三,Regex.Replace
str = Regex.Replace(str, "[\r\n\t]", string.Empty, RegexOptions.Compiled);1
方法四,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);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询