从字符串c#中删除’\’字符
我有以下代码
string line = ""; while ((line = stringReader.ReadLine()) != null) { // split the lines for (int c = 0; c < line.Length; c++) { if ( line[c] == ',' && line[c - 1] == '"' && line[c + 1] == '"') { line.Trim(new char[] {'\\'}); // <------ lineBreakOne = line.Substring(1, c - 2); lineBreakTwo = line.Substring(c + 2, line.Length - 2); } } }
我已经在我想知道的行中添加了评论网。 我想从字符串中删除所有’\’字符。 这是正确的方法吗? 我不工作。 所有\仍然在字符串中。
你可以使用:
line.Replace(@"\", "");
要么
line.Replace(@"\", string.Empty);
您可以使用String.Replace基本上删除所有出现的内容
line.Replace(@"\", "");
line = line.Replace("\\", "");
为什么不简单呢?
resultString = Regex.Replace(subjectString, @"\\", "");
尝试使用
String sOld = ...; String sNew = sOld.Replace("\\", String.Empty);
尝试更换
string result = line.Replace("\\","");
要从字符串中删除所有’\’,只需执行以下操作:
myString = myString.Replace("\\", "");
Trim
只删除字符串开头和结尾的字符,这就是你的代码不能正常工作的原因。 您应该使用Replace
:
line.Replace(@"\", string.Empty);
while ((line = stringReader.ReadLine()) != null) { // split the lines for (int c = 0; c < line.Length; c++) { line = line.Replace("\\", ""); lineBreakOne = line.Substring(1, c - 2); lineBreakTwo = line.Substring(c + 2, line.Length - 2); } }