.Net:空字符串不是清除空格字符吗?
我总是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
尝试此方法来检查空白字符串。 它与 Trim() 版本的不同之处在于它不分配新字符串。 它还使用了更扩展的空白概念。
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.
String.IsNullOrWhiteSpace 位于 .NET 4 的 BCL 中
String.IsNullOrWhiteSpace is in the BCL in .NET 4
“ ”是一个 ASCII 32 值,它与任何其他 ASCII 字符没有什么不同,只是它“看起来”是空白。
An " " is an ASCII 32 value, it is no different from any other ASCII character except it "looks" blank.
问题是您需要修剪字符串,但是如果您对空字符串调用trim(),则会收到错误。
这会出错。
您将需要执行类似的操作,
这将验证字符串不为空或空,如果有某些内容,它将修剪,然后验证其不为空。
编辑
感谢 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.
This will error out.
You will need to do something like
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
在 VB.NET 中,您需要使用如下测试:
OrElse
防止在test.Trim()
上发生 NullExceptionIn VB.NET you'll need to use a test like this:
The
OrElse
prevents the NullException from occurring on thetest.Trim()
如前所述,如果字符串为 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.如果您确实需要将仅包含空白字符的字符串与空字符串或空字符串相同,那么您可以使用像这样的扩展方法(抱歉,它是在 C# 中):
这允许您编写:
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#):
This allows you to write:
也许您只想在检查之前修剪字符串(空格)?
Perhaps you just want to trim the string (of spaces) before checking it?
如果 str 为 null,则以下内容有时可能不起作用,因为在 null 上调用方法将导致异常。
If String.IsNullOrEmpty(str.Trim()) then ' 有时会出现异常
...
万一
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
您可以添加此处讨论的扩展方法,并保持与之前相同的易用性与
IsNullOrEmpty()
一起使用。You can add an extension method such as was discussed here and keep the same ease of use you previously had with
IsNullOrEmpty()
.尝试
希望有帮助。
Try
Hope that helps.