Search code examples
c#stringtrim

Delete \ from string C#


I need to delete \, ", [ and ] from this string:

"[\"30001|410000000|400000000|PocketPC_Login|1\",\"30002|420000000|400000000|PocketPC_ChangementZone|1\",\"30003|430000000|400000000|PocketPC_Transactions|1\",\"30004|440000000|400000000|PocketPC_Gestion|1\",\"30005|450000000|400000000|PocketPC_Administration|0\",\"30006|431000000|430000000|PocketPC_TSEntrees|1\"]"

When I do this:

string.Trim(new Char[] { '"', '[', ']', "\\"})

or this:

string.Trim(new Char[] { '"', '[', ']', Convert.ToChar(92)})

the result is always the same:

"30001|410000000|400000000|PocketPC_Login|1\",\"30002|420000000|400000000|PocketPC_ChangementZone|1\",\"30003|430000000|400000000|PocketPC_Transactions|1\",\"30004|440000000|400000000|PocketPC_Gestion|1\",\"30005|450000000|400000000|PocketPC_Administration|0\",\"30006|431000000|430000000|PocketPC_TSEntrees|1"

The \ and " still there. What could I do?


Solution

  • Trim only removes items at the start or the end of a string. You should use a chain of string.Replace.

    myString
       .Replace("\\", String.Empty)
       .Replace("\"", String.Empty)
       .Replace("[", String.Empty)
       .Replace("]", String.Empty);
    

    Something else I just noticed, is that your data is likely to be JSON (the square brackets [] suggest that you have an array of strings).