How to Remove '\0' from a string in C#?
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?
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);
}
}
I use: something.TrimEnd('\0')