检查字符串是否为空或C#中的所有空格 [英] Check if string is empty or all spaces in C#

查看:349
本文介绍了检查字符串是否为空或C#中的所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何轻松检查字符串是否为空白或是否充满空格(不确定)?

How to easily check if a string is blank or full of an undetermined amount of spaces, or not?

推荐答案

如果您拥有.NET 4,使用 string .isNullOrWhiteSpace 方法

If you have .NET 4, use the string.IsNullOrWhiteSpace method:

if(string.IsNullOrWhiteSpace(myStringValue))
{
    // ...
}

如果不这样做没有.NET 4,您可以站立来修剪字符串,可以先修剪它,然后检查它是否为空。

If you don't have .NET 4, and you can stand to trim your strings, you could trim it first, then check if it is empty.

否则,您可以考虑实施它自己:

Otherwise, you could look into implementing it yourself:

。Net 3.5带代码协定的String.IsNullOrWhitespace的实现

这篇关于检查字符串是否为空或C#中的所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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