.Net:空字符串不是清除空格字符吗?

发布于 2024-07-17 03:03:03 字数 331 浏览 2 评论 0原文

我总是使用 String.IsNullOrEmpty 来检查空字符串。 最近我注意到“”不算是空字符串。 例如,

 Dim test As String = " "
    If String.IsNullOrEmpty(test) Then
        MessageBox.Show("Empty String")
    Else
        MessageBox.Show("Not Emtpy String")
    End If

它将显示“非空字符串”。 那么我们如何检查字符串中的“”或“”呢?

编辑:我不知道“”算作字符。

I've always use String.IsNullOrEmpty to check for a empty string. It recently come to my attention that a " " count as not a empty string. For example,

 Dim test As String = " "
    If String.IsNullOrEmpty(test) Then
        MessageBox.Show("Empty String")
    Else
        MessageBox.Show("Not Emtpy String")
    End If

It will show "Not Empty String". So how do we check for " " or " " in a string?

edit: I wasn't aware that " " count as character.

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

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

发布评论

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

评论(13

如果没有你 2024-07-24 03:03:03

尝试此方法来检查空白字符串。 它与 Trim() 版本的不同之处在于它不分配新字符串。 它还使用了更扩展的空白概念。

Public Function IsNullOrBlank(ByVal str as String) As Boolean
  if String.IsNullOrEmpty(str) Then
    Return True
  End IF
  For Each c In str
    IF Not Char.IsWhiteSpace(c) Then
      Return False
    End If
  Next
  Return True
End Function

Try this method to check for blank strings. It is different from the Trim() versions in that it does not allocate a new string. It also uses a more expanded notion of whitespace.

Public Function IsNullOrBlank(ByVal str as String) As Boolean
  if String.IsNullOrEmpty(str) Then
    Return True
  End IF
  For Each c In str
    IF Not Char.IsWhiteSpace(c) Then
      Return False
    End If
  Next
  Return True
End Function
雨巷深深 2024-07-24 03:03:03

String.IsNullOrWhiteSpace 位于 .NET 4 的 BCL 中

String.IsNullOrWhiteSpace is in the BCL in .NET 4

∞梦里开花 2024-07-24 03:03:03

“ ”是一个 ASCII 32 值,它与任何其他 ASCII 字符没有什么不同,只是它“看起来”是空白。

An " " is an ASCII 32 value, it is no different from any other ASCII character except it "looks" blank.

审判长 2024-07-24 03:03:03

问题是您需要修剪字符串,但是如果您对空字符串调用trim(),则会收到错误。

string.IsNullOrEmpty(s.Trim())

这会出错。

您将需要执行类似的操作,

if (Not string.IsNullOrEmpty(s) AndAlso s.Trim()!=string.Empty)

这将验证字符串不为空或空,如果有某些内容,它将修剪,然后验证其不为空。

编辑

感谢 Slough 帮助我学习 VB 语法。 我是一名 C# 人员,需要温习 vb 的语法

The problem is you need to trim the string, however if you call trim() on a null string you will get an error.

string.IsNullOrEmpty(s.Trim())

This will error out.

You will need to do something like

if (Not string.IsNullOrEmpty(s) AndAlso s.Trim()!=string.Empty)

This will verify that the string isn't null or empty, if has something it will trim and then verify its not empty.

Edit

Thanks Slough for helping me with my VB syntax. I'm a C# guy need to brush up on vb's syntax

爱格式化 2024-07-24 03:03:03

在 VB.NET 中,您需要使用如下测试:

If String.IsNullOrEmpty(test) OrElse String.IsNullOrEmpty(test.Trim()) Then

OrElse 防止在 test.Trim() 上发生 NullException

In VB.NET you'll need to use a test like this:

If String.IsNullOrEmpty(test) OrElse String.IsNullOrEmpty(test.Trim()) Then

The OrElse prevents the NullException from occurring on the test.Trim()

我要还你自由 2024-07-24 03:03:03
public static bool IsNullOrWhite(string s)
{
  if (String.IsNullOrEmpty(s))
    return true;

  for (int i = 0; i < s.Length; i++)
    if (!Char.IsWhiteSpace(s, i))
      return false;

  return true;
}
public static bool IsNullOrWhite(string s)
{
  if (String.IsNullOrEmpty(s))
    return true;

  for (int i = 0; i < s.Length; i++)
    if (!Char.IsWhiteSpace(s, i))
      return false;

  return true;
}
季末如歌 2024-07-24 03:03:03

如前所述,如果字符串为 null,则调用 Trim() 将抛出 NullReferenceException。 我有时使用 Regex.IsMatch(test, "^ +$") (希望参数顺序正确)来测试一个或多个空格。 ^ 和 $ 确保匹配整个字符串。

As was mentioned, calling Trim() will throw a NullReferenceException if the string is null. I sometimes use Regex.IsMatch(test, "^ +$") (hope I have the parameter order right) to test for one or more spaces. The ^ and $ make sure you're matching the entire string.

乖乖公主 2024-07-24 03:03:03

如果您确实需要将仅包含空白字符的字符串与空字符串或空字符串相同,那么您可以使用像这样的扩展方法(抱歉,它是在 C# 中):

namespace ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmptyOrWhitespace(this string str)
        {
            if (string.IsNullOrEmpty(str)) return true;
            return (str.Trim().Length == 0);
        }
    }
}

这允许您编写:

using ExtensionMethods;

string s1 = "  ";
string s2 = "some string";

s1.IsNullOrEmptyOrWhitespace(); //-> returns true
s2.IsNullOrEmptyOrWhitespace(); //-> returns false

If you really need to treat strings containing only whitespace character the same as empty or null strings, then you could use an extension method like this one (sorry, it's in C#):

namespace ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmptyOrWhitespace(this string str)
        {
            if (string.IsNullOrEmpty(str)) return true;
            return (str.Trim().Length == 0);
        }
    }
}

This allows you to write:

using ExtensionMethods;

string s1 = "  ";
string s2 = "some string";

s1.IsNullOrEmptyOrWhitespace(); //-> returns true
s2.IsNullOrEmptyOrWhitespace(); //-> returns false
山有枢 2024-07-24 03:03:03
If test Is Nothing OrElse test.Trim().Length = 0 Then ...
If test Is Nothing OrElse test.Trim().Length = 0 Then ...
可爱咩 2024-07-24 03:03:03

也许您只想在检查之前修剪字符串(空格)?

Dim test As String = " "
If test.Trim().Length = 0 Then // Best option as long as string is guaranteed not to be null.
//  If test = Nothing OrElse test.Trim().Length = 0 Then // If string can be null.
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If

Perhaps you just want to trim the string (of spaces) before checking it?

Dim test As String = " "
If test.Trim().Length = 0 Then // Best option as long as string is guaranteed not to be null.
//  If test = Nothing OrElse test.Trim().Length = 0 Then // If string can be null.
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If
白首有我共你 2024-07-24 03:03:03
If String.IsNullOrEmpty(str) OrElse str.Trim().Length = 0 Then
  ' ..
End If

如果 str 为 null,则以下内容有时可能不起作用,因为在 null 上调用方法将导致异常。
If String.IsNullOrEmpty(str.Trim()) then ' 有时会出现异常
...
万一

If String.IsNullOrEmpty(str) OrElse str.Trim().Length = 0 Then
  ' ..
End If

The following may not work sometime if the str is null as calling a method on null will result in exception.
If String.IsNullOrEmpty(str.Trim()) Then ' exception sometimes
...
End If

哆兒滾 2024-07-24 03:03:03

您可以添加此处讨论的扩展方法,并保持与之前相同的易用性与 IsNullOrEmpty() 一起使用。

You can add an extension method such as was discussed here and keep the same ease of use you previously had with IsNullOrEmpty().

在风中等你 2024-07-24 03:03:03

尝试

Dim test As String = " "
If String.IsNullOrEmpty(test.Trim()) Then
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If

希望有帮助。

Try

Dim test As String = " "
If String.IsNullOrEmpty(test.Trim()) Then
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If

Hope that helps.

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