removing special characters

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?
 
J

Jon Skeet

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

Austin Ehlers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top