空字符串比较 [英] Empty string comparisons

查看:116
本文介绍了空字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


目前我的记忆空白。我用C#编写了多年的

,现在需要在VB.NET中做一些事情,所以原谅我这样一个原始问题。


在C#中,我通过以下语法测试字符串是否有值:


if(thisString.Trim()=="" ;)

{

//如果是真的

某事()

}

else

{

//如果错误

somethingelse()

}

现在,在VB.NET中,等效的东西似乎不起作用:


如果thisString.Trim()=""然后

''如果是真的

东西()

否则

''如果错误

somethingelse()

结束如果


我无法通过VB比较的错误返回。什么是最好的

方法来测试字符串是空还是空?

问候,

Neville Lang

Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such a
primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Trim() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim() = "" Then
'' if true
something()
Else
'' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang

推荐答案

嗨Neville


不知道这是否最好,但它确实有效:


如果testStr为Nothing或Len(Trim(testStr))= 0那么

''0长度或空字符串

Else

''字符串已初始化且不为空

结束如果


干杯

Martin


Neville Lang写道:
Hi Neville

Don''t know whether this is ''best'' or not but it certainly works:

If testStr Is Nothing or Len(Trim(testStr)) = 0 Then
'' 0 length or null string
Else
'' string is initialised and not empty
End if

Cheers
Martin

Neville Lang wrote:

大家好,


我的内存空白在这一刻。我用C#编写了多年的

,现在需要在VB.NET中做一些事情,所以原谅我这样一个原始问题。


在C#中,我通过以下语法测试字符串是否有值:


if(thisString.Trim()=="" ;)

{

//如果是真的

某事()

}

else

{

//如果错误

somethingelse()

}

现在,在VB.NET中,等效的东西似乎不起作用:


如果thisString.Trim()=""然后

''如果是真的

东西()

否则

''如果错误

somethingelse()

结束如果


我无法通过VB比较的错误返回。用于测试字符串是空还是空的最佳

方法是什么?


问候,

Neville Lang
Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such a
primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Trim() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim() = "" Then
'' if true
something()
Else
'' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang




Neville Lang ha scritto:


Neville Lang ha scritto:


Now ,在VB.NET中,等效似乎不起作用:


如果thisString.Trim()=""然后

''如果是真的

东西()

否则

''如果错误

somethingelse()

结束如果
Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim() = "" Then
'' if true
something()
Else
'' if false
somethingelse()
End If



我通常使用:


Dim s As String =""

如果s.Trim = String.Empty则

MsgBox(为空)

Else

MsgBox(不是空的)

结束如果


但你的版本对我来说也很好(除非我是想看看

东西)...

Tommaso


I usually use:

Dim s As String = ""
If s.Trim = String.Empty Then
MsgBox("is empty")
Else
MsgBox("is not empty")
End If

but your version also looks fine to me (unless I am missing to see
something)...
Tommaso


>

我无法通过VB比较的错误返回。用于测试字符串是空还是空的最佳

方法是什么?


问候,

Neville Lang
>
I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang


您的问题可能在其他地方 - 您发布的VB代码确切地说是相当于C#代码的


-

David Anton
www.tangiblesoftwaresolutions.com

即时C#:VB到C#转换器

即时VB:C#到VB转换器

即时C ++:C#/ VB到C ++转换器

即时Python:VB到Python转换器

" Neville Lang"写道:
Your problem probably lies elsewhere - the VB code you posted is the exact
equivalent to the C# code.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Neville Lang" wrote:

大家好,


目前我的内存空白。我用C#编写了多年的

,现在需要在VB.NET中做一些事情,所以原谅我这样一个原始问题。


在C#中,我通过以下语法测试字符串是否有值:


if(thisString.Trim()=="" ;)

{

//如果是真的

某事()

}

else

{

//如果错误

somethingelse()

}

现在,在VB.NET中,等效的东西似乎不起作用:


如果thisString.Trim()=""然后

''如果是真的

东西()

否则

''如果错误

somethingelse()

结束如果


我无法通过VB比较的错误返回。用于测试字符串是空还是空的最佳

方法是什么?


问候,

Neville Lang


这篇关于空字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆