忽略 null 语句,仅将方法应用于填充的字符串

发布于 2024-11-16 02:51:55 字数 544 浏览 0 评论 0原文

如何忽略 null 语句,并仅应用一种方法来删除填充的字符串中的特殊字符。

Answer1 = RemoveSpecialChars(doc.SelectSingleNode("/Main/Answer[@answerid='1']").Attributes["keypress"].Value);                 
Answer2 = RemoveSpecialChars(doc.SelectSingleNode("/Main/Answer[@answerid='2']").Attributes["keypress"].Value);

 public string RemoveSpecialChars(string input)
       {

           return Regex.Replace(input, @"[^0-9a-zA-Z\._]", string.Empty);
       }

发生的情况是,当用户按下并发送答案一,而答案二没有任何内容时,我会得到一个异常,因为该方法试图在空字符串上运行。如果答案 2 为空,通过答案 1 的最佳方式是什么?

How do you ignore a null statement, and only apply a method to remove special characters to only strings that are populated.

Answer1 = RemoveSpecialChars(doc.SelectSingleNode("/Main/Answer[@answerid='1']").Attributes["keypress"].Value);                 
Answer2 = RemoveSpecialChars(doc.SelectSingleNode("/Main/Answer[@answerid='2']").Attributes["keypress"].Value);

 public string RemoveSpecialChars(string input)
       {

           return Regex.Replace(input, @"[^0-9a-zA-Z\._]", string.Empty);
       }

What's happening, is when the user presses and sends an answer one, and nothing for answer two I get an exception, because the method is trying to run on an empty string. What is the best way to pass answer1, if answer 2 is empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-11-23 02:51:55

听起来您的问题不在 RemoveSpecialChars 方法中,而是在 SelectSingleNode 的返回值(可能是 null)或 < code>Attributes["keypress"] 属性(也可能为 null)。

上述任何情况都会导致 NullReferenceException。下面是重写的代码,以防止第一个问题,这可能会导致问题:

var node1 = doc.SelectSingleNode("/Main/Answer[@answerid='1']");
var node2 = doc.SelectSingleNode("/Main/Answer[@answerid='2']");

Answer1 = node1 == null ? null : RemoveSpecialChars(node1.Attributes["keypress"].Value);
Answer2 = node2 == null ? null : RemoveSpecialChars(node2.Attributes["keypress"].Value);

更新

要防止 null keypress 属性,您可以

Answer1 = node1 == null || node1.Attributes["keypress"] == null
            ? null 
            : RemoveSpecialChars(node1.Attributes["keypress"].Value);

执行相同的操作答案2。

It sounds like your problem is not in the RemoveSpecialChars method, but rather in the return value of SelectSingleNode (which may be null) or the Attributes["keypress"] attribute (which may also be null).

Any of the above will result in a NullReferenceException. Here's rewritten code to guard against the first, which is probably causing the issue:

var node1 = doc.SelectSingleNode("/Main/Answer[@answerid='1']");
var node2 = doc.SelectSingleNode("/Main/Answer[@answerid='2']");

Answer1 = node1 == null ? null : RemoveSpecialChars(node1.Attributes["keypress"].Value);
Answer2 = node2 == null ? null : RemoveSpecialChars(node2.Attributes["keypress"].Value);

Update:

To guard against a null keypress attribute, you would do

Answer1 = node1 == null || node1.Attributes["keypress"] == null
            ? null 
            : RemoveSpecialChars(node1.Attributes["keypress"].Value);

and the same for Answer2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文