当前位置: 代码网 > it编程>编程语言>C# > C#判断字符串不等于空的方法小结

C#判断字符串不等于空的方法小结

2024年07月03日 C# 我要评论
方法1使用逻辑运算符和string.isnullorempty方法string mystring = "123"; // 假设要检查的字符串 if (!string.isnullorempty(my

方法1

使用逻辑运算符和string.isnullorempty方法

string mystring = "123"; // 假设要检查的字符串  
if (!string.isnullorempty(mystring))  
{  
    // 字符串不是null,也不是空字符串  
}

方法2

使用逻辑运算符和string.isnullorwhitespace方法(如果还要检查空白字符串,如只包含空格、制表符或换行符的字符串)

string mystring ="123"; // 假设这是要检查的字符串  
if (!string.isnullorwhitespace(mystring))  
{  
    // 字符串不是null,也不是空字符串或仅包含空白字符  
}

方法3

使用逻辑运算符和直接比较(只检查空字符串,不检查null)

string mystring = "123"; // 假设这是要检查的字符串  
if (mystring != null && mystring != "")  
{  
    // 字符串不是null,也不是空字符串  
}

方法4

使用c# 8.0及更高版本的空合并运算符(null-conditional operator)和逻辑运算符(仅当需要提供一个默认值时使用)

string mystring ="123"; // 假设这是要检查的字符串  
string nonnulloremptystring = mystring ?? ""; // 如果mystring是null,则nonnulloremptystring将被设置为""  
if (nonnulloremptystring != "")  
{  
    // 字符串不是空字符串(但可能是null,但在这个例子中已经被转换成了"")  
}

但是,请注意,上面的方法4只检查了空字符串,并没有检查原始字符串是否为null。如果需要同时检查null和空字符串,最好使用第一种或第二种方法。

测试代码

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;

namespace windowsformsapplication1
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }

        private void strfun1()
        {
            string mystring ="123"; // 假设要检查的字符串  
            if (!string.isnullorempty(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串不是null,也不是空字符串");
            }
            mystring = null;
            if (string.isnullorempty(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串是null或是空字符串");
            }
            mystring = "";
            if (string.isnullorempty(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串是null或是空字符串");
            }
        }

        private void strfun2()
        {
            string mystring ="123"; // 假设这是要检查的字符串  
            if (!string.isnullorwhitespace(mystring))
            {
                // 字符串不是null,也不是空字符串或仅包含空白字符  
                messagebox.show("字符串不是null,也不是空字符串或仅包含空白字符");
            }
            mystring = null;
            if (string.isnullorwhitespace(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串是null或是空字符串或仅包含空白字符");
            }
            mystring = "";
            if (string.isnullorwhitespace(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串是null或是空字符串或仅包含空白字符");
            }
            mystring = "  ";
            if (string.isnullorwhitespace(mystring))
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串是null或是空字符串或仅包含空白字符");
            }
        }
        private void strfun3()
        {
            string mystring = "123"; // 假设要检查的字符串  
            if (mystring != null && mystring != "")
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串不是null,也不是空字符串"); 
            }
            mystring = null;
            if (mystring == null )
            {
                // 字符串是null 
                messagebox.show("字符串是null");
            }
            mystring = "";
            if (mystring == "")
            {
                // 字符串是空字符串  
                messagebox.show("字符串是空字符串");
            }
        }

        private void strfun4()
        {
            string mystring = "123"; // 假设要检查的字符串  

            string nonnulloremptystring = mystring ?? ""; // 如果mystring是null,则nonnulloremptystring将被设置为""  


            if (nonnulloremptystring != null && nonnulloremptystring != "")
            {
                // 字符串不是null,也不是空字符串  
                messagebox.show("字符串不是null,也不是空字符串");
            }
            if (nonnulloremptystring == null)
            {
                // 字符串是null 
                messagebox.show("字符串是null");
            }
            if (nonnulloremptystring == "")
            {
                // 字符串是空字符串  
                messagebox.show("字符串是空字符串");
            }
        }

        private void button1_click(object sender, eventargs e)
        {
            strfun1();
        }

        private void button2_click(object sender, eventargs e)
        {
            strfun2();
        }

        private void button3_click(object sender, eventargs e)
        {
            strfun3();
        }

        private void button4_click(object sender, eventargs e)
        {
            strfun4();
        }
    }
}

到此这篇关于c#判断字符串不等于空的方法小结的文章就介绍到这了,更多相关c#判断字符串内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com