简体   繁体   中英

Format String “Hello\World” to “HelloWorld”

I loop the value on first column each row of datagridview, and the format has "\\" in the middle, how do we convert convert the string without "\\"

ex.

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

etc

String handling

string hello = "Hello\\World";
string helloWithoutBackslashes = hello.Replace("\\",string.Empty);

or, using the @ operator

string hi = @"Hi\There";
string hiWithoutBackslashes = hi.Replace(@"\",string.Empty);

I thought I would mix it up a bit.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM