How to Remove '\0' from a string in C#?

Viewed 83524

I would like to know how to remove '\0' from a string. This may be very simple but it's not for me since I'm a new C# developer.

I've this code:

public static void funcTest (string sSubject, string sBody)
{
    Try
      {
        MailMessage msg = new MailMessage(); // Set up e-mail message.
        msg.To = XMLConfigReader.Email;
        msg.From = XMLConfigReader.From_Email;
        msg.Subject = sSubject;
        msg.body="TestStrg.\r\nTest\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\r\n";     
      }
    catch (Exception ex) 
      {
        string sMessage = ex.Message;     
        log.Error(sMessage, ex);   
      }
}

But what I want is:

msg.body="TestStrg.\r\nTest\r\n";

So, is there a way to do this with a simple code?

11 Answers

It may have a faster result if you use LINQ

str.TakeWhile(c => c != '\0');

Linq has a better and faster performance.

Related