我在datagridview的每一行的第一列上循环值,并且格式在中间有“\”,我们如何转换为没有“\”的字符串转换
离。
"Hello\World" to "HelloWorld"
"Hi\There" to "HiThere""
等
答案 0 :(得分:8)
string hello = "Hello\\World";
string helloWithoutBackslashes = hello.Replace("\\",string.Empty);
或使用@运算符
string hi = @"Hi\There";
string hiWithoutBackslashes = hi.Replace(@"\",string.Empty);
答案 1 :(得分:1)
我以为我会混淆一下。
public class StringCleaner
{
private readonly string dirtyString;
public StringCleaner(string dirtyString)
{
this.dirtyString = dirtyString;
}
public string Clean()
{
using (var sw = new System.IO.StringWriter())
{
foreach (char c in dirtyString)
{
if (c != '\\') sw.Write(c);
}
return sw.ToString();
}
}
}