将字符串“Hello \ World”格式化为“HelloWorld”

时间:2009-12-15 23:02:52

标签: c#

我在datagridview的每一行的第一列上循环值,并且格式在中间有“\”,我们如何转换为没有“\”的字符串转换

离。

"Hello\World" to  "HelloWorld"
"Hi\There" to "HiThere""

2 个答案:

答案 0 :(得分:8)

String handling

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