且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查多个字符串值是否为空或一次性?

更新时间:2023-01-31 14:31:49

如果您的数据来自数据库,请将其与 DBNull.Value进行比较 [ ^ ]
If your data comes from database, compare it to DBNull.Value[^]






在List< string>中添加你的所有字符串值。



例如,

List< string> s = new List< string>();



s.add(你的字符串值);

最多n次,你可以在for循环中执行。



然后像这样检查,



Hi,

Add your all string value in List<string> .

say for example,
List<string> s = new List<string>();

s.add(your string value);
up to n time, you can do it in for loop.

Then check like this,

List<string> s1 = (from a in s
                    where string.IsNullOrEmpty(a)
                    select a.ToString()).ToList();







检查s1.Count()。如果> 0表示您的字符串为空白值。




Check s1.Count(). If > 0 means you have string with blank value.


Quote:

以下示例检查三个字符串并确定每个字符串是否有值,是否为空字符串,或者为空。

The following example examines three strings and determines whether each string has a value, is an empty string, or is null.





代码示例





Code Sample

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string s1 = "abcd";
            string s2 = "";
            string s3 = null;

            Console.WriteLine("String s1 {0}.", Test(s1));
            Console.WriteLine("String s2 {0}.", Test(s2));
            Console.WriteLine("String s3 {0}.", Test(s3));
            Console.ReadLine();
        }

        public static String Test(string s)
        {
            if (String.IsNullOrEmpty(s))
                return "is null or empty";
            else
                return String.Format("(\"{0}\") is neither null nor empty", s);
        }
        }
    }