方法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#判断字符串内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论