C#判斷字符串不等于空的方法小結(jié)
方法1
使用邏輯運(yùn)算符和string.IsNullOrEmpty方法
string myString = "123"; // 假設(shè)要檢查的字符串 if (!string.IsNullOrEmpty(myString)) { // 字符串不是null,也不是空字符串 }
方法2
使用邏輯運(yùn)算符和string.IsNullOrWhiteSpace方法(如果還要檢查空白字符串,如只包含空格、制表符或換行符的字符串)
string myString ="123"; // 假設(shè)這是要檢查的字符串 if (!string.IsNullOrWhiteSpace(myString)) { // 字符串不是null,也不是空字符串或僅包含空白字符 }
方法3
使用邏輯運(yùn)算符和直接比較(只檢查空字符串,不檢查null)
string myString = "123"; // 假設(shè)這是要檢查的字符串 if (myString != null && myString != "") { // 字符串不是null,也不是空字符串 }
方法4
使用C# 8.0及更高版本的空合并運(yùn)算符(null-conditional operator)和邏輯運(yùn)算符(僅當(dāng)需要提供一個(gè)默認(rèn)值時(shí)使用)
string myString ="123"; // 假設(shè)這是要檢查的字符串 string nonNullOrEmptyString = myString ?? ""; // 如果myString是null,則nonNullOrEmptyString將被設(shè)置為"" if (nonNullOrEmptyString != "") { // 字符串不是空字符串(但可能是null,但在這個(gè)例子中已經(jīng)被轉(zhuǎn)換成了"") }
但是,請(qǐng)注意,上面的方法4只檢查了空字符串,并沒有檢查原始字符串是否為null。如果需要同時(shí)檢查null和空字符串,最好使用第一種或第二種方法。
測(cè)試代碼
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"; // 假設(shè)要檢查的字符串 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"; // 假設(shè)這是要檢查的字符串 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"; // 假設(shè)要檢查的字符串 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"; // 假設(shè)要檢查的字符串 string nonNullOrEmptyString = myString ?? ""; // 如果myString是null,則nonNullOrEmptyString將被設(shè)置為"" 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(); } } }
到此這篇關(guān)于C#判斷字符串不等于空的方法小結(jié)的文章就介紹到這了,更多相關(guān)C#判斷字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm子窗體訪問父窗體控件的實(shí)現(xiàn)方法
WinForm子窗體訪問父窗體控件的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer
這篇文章主要介紹了分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer,需要的朋友可以參考下2015-11-11C#將Word轉(zhuǎn)換成PDF方法匯總(基于Office和WPS)
這篇文章主要匯總了C#將Word轉(zhuǎn)換成PDF方法,基于Office和WPS的兩種解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05