removing special characters

  • Thread starter Thread starter Mark Bosley
  • Start date Start date
M

Mark Bosley

i'm reading from a database and found special character showing up as \0
or
when i serialize to xml.

trim does not work and using the string.Replace doesn't either.

how do i remove these?
 
Mark Bosley said:
i'm reading from a database and found special character showing up as \0
or
when i serialize to xml.

trim does not work and using the string.Replace doesn't either.

how do i remove these?

Trim should work just fine, as should string.Replace - could you give a
short but complete example of them not working? Here's one showing them
working:

using System;

public class Test
{
static void Main ()
{
string original = "hello\0 there\0\0\0";
string trimmed = original.Trim ('\0');
string replaced = original.Replace ("\0", "");

Console.WriteLine (original.Length);
Console.WriteLine (trimmed.Length);
Console.WriteLine (replaced.Length);
}
}
 
i'm reading from a database and found special character showing up as \0
or
when i serialize to xml.

trim does not work and using the string.Replace doesn't either.

how do i remove these?

My guess is that you are calling it like this:

stringvar.Trim("\0");

However, in C#, strings are immutable, so that call actually returns a
new string. Call it like this:

stringvar=stringvar.Trim("\0");

'Lib
 
Back
Top