如何检查多个字符串值是否为空或一次性? [英] how to check multiple string value are empty or not at one go ?

查看:327
本文介绍了如何检查多个字符串值是否为空或一次性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,

我有大约15个字符串值..每个我想检查他们是否为空,如果它们是空的,那么我想显示一个错误,这个字符串是空的..但是对于每一个我检查isnullor为空并返回错误信息...你能以任何方式检查所有值一次性检查每次所有时间

Hii,
I have around 15 string values .. n for each i wanted to check wether they are empty or not if they are empty then i want to display an error that this string is empty .. but for each of this i am checking isnullor empty and returning an error message ... do you any way in which we can check all values at one go insted of checking each all time

推荐答案

如果您的数据来自数据库,请将其与 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);
        }
        }
    }


这篇关于如何检查多个字符串值是否为空或一次性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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