55

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Davideg
  • 841
  • 4
  • 14
  • 23

11 Answers11

64

It seems you just want the string.Replace function (static method).

var cleaned = input.Replace("\0", string.Empty);

Edit: Here's the complete code, as requested:

public static void funcTest (string sSubject, string sBody)
{
    try
    {
        MailMessage msg = new MailMessage();
        msg.To = XMLConfigReader.Email;
        msg.From = XMLConfigReader.From_Email;
        msg.Subject = sSubject;
        msg.Body = sBody.Replace("\0", string.Empty);
    }
    catch (Exception ex) 
    {
        string sMessage = ex.Message;     
        log.Error(sMessage, ex);   
    }
}
Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • Hello Noldorin.... So how I add this code line into my function. Because when I add a new line of code to the function, it is not executed and I don`t know why.. Please help me how to insert this line of code you provide into my function in order to be executed normally. Thank you a lot... – Davideg Aug 02 '10 at 13:19
  • When I alter the line: msg.body = sBody.Replace("\0", string.Empty); a bulid error is generated: 'System.Web.Mail.MailMessage.body' is inaccessible due to its protection level. 1)Why is that? 2)When I add a new code into my project. It doesn`t executed and step over it and I don`t know why? can you tell me why? Thank you v.much... – Davideg Aug 02 '10 at 13:40
  • Ok man I think this code will make it.. But when I add a new code to my function, it doesn`t executed at runtime.. I don`t know what to do? can you help me with this? – Davideg Aug 02 '10 at 13:54
50

I use: something.TrimEnd('\0')

Contango
  • 76,540
  • 58
  • 260
  • 305
Sith2021
  • 3,245
  • 30
  • 22
13

This was the first result in Bing, and it didn't have my preferred method, which is

string str = "MyString\0\0\0\0".Trim('\0');
Drew
  • 184
  • 2
  • 8
9

you just need to Replace it

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".Replace("\0", string.Empty);    
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • here net connection is too slow :( to post soon – anishMarokey Aug 02 '10 at 12:42
  • 10
    @Noldorin, that is incredibly rude for a difference of 3 minutes. – Anthony Pegram Aug 02 '10 at 12:42
  • @Anthony: Not really. a) It's 4 minutes, b) It's bad etiquette to post an identical answer once several have already been posted, just to milk rep points. If I answer substantially late when a question has already been answered, I delete my response appropiately. – Noldorin Aug 02 '10 at 12:44
6

Try this:

msg.Body = msg.Body.Replace("\0", "");
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • It seems it should work. But I have now another problem & I need your help. When I add a new line of cod to any class in my c# project. This line of code is not executed at run time and the debugger step over it. 1) How to make the new code to be executed? 2) Is there a property in Visual studio 2005? 3) Or is it something related to the c# project? Thanks in advance for any try to help.. – Davideg Aug 03 '10 at 04:14
  • 1
    Thats sounds like the project is not being built, and so any new changes made are ignored. Check your project is set to build in the Configuration Manager – Iain Ward Aug 03 '10 at 08:51
5

It may have a faster result if you use LINQ

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

Linq has a better and faster performance.

redParrot
  • 440
  • 1
  • 7
  • 14
4
msg.body = sBody.Replace("\0", "");
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
3
var str = "TestStrg.\r\nTest\0\0\0\0\0\0\0\0\0\r\n".Replace("\0", "");

String.Replace() will replace all the \0's with an empty string, thereby removing them.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • 3
    I'm not a C# guy, but out of curiosity what is the `@` for? – Chris Aug 02 '10 at 12:31
  • 2
    Actually won't work with the @ symbol because that means it will ignore the escape character and interpret the string as "backslash-zero" rather than escaping it to the null character. – Martin Harris Aug 02 '10 at 12:32
  • @ = Escape sequences should NOT be processed. – Rippo Aug 02 '10 at 12:32
  • @Chris: `@` is for taking in consideration the slash as a symbol. The answer is wrong, `@` shouldn't be present here, because one `\0` simbol is replaced, not two `\ ` and `0`. – serhio Aug 02 '10 at 12:34
  • So `@"blah\t"` is like Python's `r"blah\t"`? – Chris Aug 02 '10 at 16:16
2

Keep it stupidly simple =*

return stringValue.Substring(0, stringValue.IndexOf('\0'));
MFedatto
  • 134
  • 2
  • 16
  • This is exactly what I needed. I had some garbage after the first \0 and that remained with the Replace methods from above. – Gerhard Dec 10 '15 at 01:03
1

I know I'm late here but, while String.Replace works most of the time I have found that I like the regex version of this much better and is more reliable in most cases

using System.Text.RegularExpressions;
...

 msg.body=Regex.Replace("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","\0");
lostcny
  • 31
  • 2
1

This line should work:

string result = Regex.Replace(input, "\0", String.Empty);
Raveendra M Pai
  • 445
  • 2
  • 10
  • 27