标签: isnullorempty

使用 PHP 的 SimpleXML 检测空的 XML 节点

首先让我说我不精通解析 XML 和/或编写 PHP。我一直在研究和拼凑我正在做的事情,但我被卡住了。

我正在尝试创建一个基本的 if/else 语句:如果节点不为空,则写入节点的内容。

这是我正在调用的 XML 片段:

<channel>
    <item>
      <title>This is a test</title>
      <link />
      <description>Test description</description>
      <category>Test category</category>
      <guid />
    </item>
  </channel>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的 PHP:

<?php
    $alerts = simplexml_load_file('example.xml');
    $guid = $alerts->channel->item->guid;

    if ($guid->count() == 0) {
    print "// No alert";
}
    else {
        echo "<div class='emergency-alert'>".$guid."</div>";
}

?>
Run Code Online (Sandbox Code Playgroud)

显然,“guid”是一个空节点,但它正在返回:

<div class="emergency-alert"> </div>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?:(

PS,我试过 hasChildren() ,但也没有用。

php simplexml nodes isnullorempty

3
推荐指数
2
解决办法
8758
查看次数

如何在sql server中编写类似于ISNULL的IsNullOrEmpty方法

我想在sql server中有类似于ISNULL()的函数,除了它应该检查表达式为null和空.

如果第一个参数为null或为空,则返回第二个参数.

有人能帮我吗?

sql-server isnull isnullorempty

3
推荐指数
1
解决办法
9066
查看次数

如何检查DateTime对象是否为空

我希望验证一个DateTime变量以确保它在 UI 上不是空白的。字符串等效检查将是String.IsNullOrEmpty(),但是我将如何处理我的DateTime变量?

c# datetime isnullorempty

3
推荐指数
1
解决办法
1万
查看次数

如何检查许多参数是否为 null 或空字符串?- C#

我有下面的方法,我需要检查参数是否为空或为空。

    public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
    {
        _Where = " WHERE " + field + " " + operat + " @" + field + "1 " + andOr + " " + field2 + " " + operat2 + " @" + field2 + "2 ";
        _Params.Add(field + "1", value);
        _Params.Add(field2 + "2", value2);
        return this;
    }
Run Code Online (Sandbox Code Playgroud)

我找到了 string.IsNullOrWhiteSpace 方法,但这需要这么多代码:

                   if (string.IsNullOrWhiteSpace(field))
            throw new ArgumentException("field Cannot be null …
Run Code Online (Sandbox Code Playgroud)

c# string null isnullorempty

3
推荐指数
1
解决办法
6439
查看次数

String.IsNullOrEmpty未检测到C#中的转义序列NULL\0

我尝试了以下if语句但是如果无法检测到NULL

void Main()
{
    string str = "\0".Trim();
    if (string.IsNullOrEmpty(str))
    {
        "Empty".Dump();
    }
    else
    {
        "Non-Empty".Dump();
    }
}
Run Code Online (Sandbox Code Playgroud)

请参考LinqPad快照

在此输入图像描述

我得到了输出

Non-Empty
Run Code Online (Sandbox Code Playgroud)

我不知道它是怎么失败的.请帮助我.

c# string null escaping isnullorempty

3
推荐指数
1
解决办法
558
查看次数

如何避免Pyqt5的Lineedit中出现Null或空值

我使用 python 使用 PyQT5 创建了一个表单,在该表单中我通过 QLineEdit 从用户那里获取价值。

我的问题是用户不应将表单中的任何字段留空。

如何避免空行编辑?

python isnullorempty qlineedit pyqt5

3
推荐指数
1
解决办法
2451
查看次数

string.IsNullOrEmpty()似乎不能处理类中类的字符串

首先,我想我知道发生了什么,但我想我会把这个问题带到这里进行一些讨论,看看是否有人对这个问题有"回答",而不是我在想什么.因为,这对我来说并不完全有意义.

我发现在为异常创建错误日志时,我这样做并且它无法正常工作:

catch( Exception ex )
{
   LogException( ex.Message );
   if ( !string.IsNullOrEmpty( ex.InnerException.Message ) )
   {
      LogInnerException( ex.InnerException.Message );
   }
}
Run Code Online (Sandbox Code Playgroud)

瞧,当我跑这个时,我经常得到一个NullReferenceException.咦?

我正在检查null,对吗?

现在,我必须使用这个:

   if ( ex.InnerException != null && !string.IsNullOrEmpty( ex.InnerException.Message ) 
Run Code Online (Sandbox Code Playgroud)

但这似乎是违反直觉的,也会产生反作用.因为,如果我这样做:

   if ( !string.IsNullOrEmpty( null ) )
Run Code Online (Sandbox Code Playgroud)

这根本不会给我任何问题.如果ex.InnerException为null,那么当然ex.InnerException.Message为null,对吗?

显然不是.

我写了一个完整的控制台应用程序来重现这个.如果你

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace stringisnullorempty
{
    class Program
    {
        static void Main( string[] args )
        {
            if ( !string.IsNullOrEmpty( null ) )
            {
                Console.WriteLine( "Ha ha ha, right...." );
            }

            MyBClass …
Run Code Online (Sandbox Code Playgroud)

.net c# string nullreferenceexception isnullorempty

2
推荐指数
1
解决办法
6520
查看次数

Cocoa是否具有类似于.NET的String.IsNullOrEmpty的功能?

可能重复:
如何在Objective C中测试字符串是否为空?

我一直在查看Apple的文档中的一个相对基本的函数,我无法想象它们没有函数来查看字符串是否为空/空或空白空间.我也谷歌搜索过这个.我发现的唯一的事情是人们就如何创建自己的函数来测试空字符串提供建议.

在.NET中,我只能说

If (String.IsNullOrEmpty(txtTextBox.Text)) {
// Action
}
Run Code Online (Sandbox Code Playgroud)

Cocoa是否与.NET的String类中的IsNullOrEmpty等效?

.net string cocoa isnullorempty

2
推荐指数
1
解决办法
224
查看次数

IsNullorEmpty combobox.selectedvalue

我试图将一个combobox.selectedValue设置为一个有效的字符串,但是当它的nullorempty错误输出时.我尝试了以下代码无济于事:

        if (string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
        {
            document = "other";
        }

        else
        {
            document = docRelComboBox.SelectedValue.ToString();
        }
Run Code Online (Sandbox Code Playgroud)

组合框是数据绑定的,但理论上它在某些情况下可能是无空的,我需要能够在那些时间传递其他值.任何帮助都会很棒.

c# combobox isnullorempty

1
推荐指数
1
解决办法
4656
查看次数

如何在 Kotlin when 表达式中使用 isNullOrEmpty 等检查

与许多其他编程语言一样,Kotlin 提供了isNullOrEmpty等检查,它允许简单地检查myString.isNullOrEmpty().

有没有办法将这样的检查与when 表达式结合起来?默认情况下,表达式仅允许显式字符串。这样的事情可能吗?:

when (myString) {
    "1" -> print("1")
    "2" -> print("1")
    myString.isNullOrEmpty() -> print("null or empty") //This is what I am looking for
    else -> print("None of them")
}
Run Code Online (Sandbox Code Playgroud)

string case-when isnullorempty kotlin

1
推荐指数
1
解决办法
1849
查看次数