问题
I have the following code
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);
}
}
}
I have added a comment net to the line I am wondering about. I want to remove all '\' chars from the string. Is this the correct way to to this? I doesnt work. All \ are still in the string.
回答1:
You could use:
line.Replace(@"\", "");
or
line.Replace(@"\", string.Empty);
回答2:
You can use String.Replace which basically removes all occurrences
line.Replace(@"\", "");
回答3:
Why not simply this?
resultString = Regex.Replace(subjectString, @"\\", "");
回答4:
line = line.Replace("\\", "");
回答5:
Try using
String sOld = ...;
String sNew = sOld.Replace("\\", String.Empty);
回答6:
to remove all '\' from a string, simply do the following:
myString = myString.Replace("\\", "");
回答7:
Try to replace
string result = line.Replace("\\","");
回答8:
Trim
only removes characters at the beginning and the end of the string, that's why your code doesn't quite work. You should use Replace
instead:
line.Replace(@"\", string.Empty);
回答9:
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);
}
}
来源:https://stackoverflow.com/questions/8383409/remove-char-from-string-c-sharp