本文介绍了如何从C#中的字符串中删除"\ 0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从字符串中删除'\ 0'.这可能很简单,但由于我是新的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.

我有此代码:

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

但是我想要的是:

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

那么,有没有办法用一个简单的代码来做到这一点?

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

推荐答案

似乎您只需要 string.Replace 函数(静态方法).

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

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

这是要求的完整代码:

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

这篇关于如何从C#中的字符串中删除"\ 0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:25