C#判斷字符串不等于空的方法小結(jié)
方法1
使用邏輯運算符和string.IsNullOrEmpty方法
string myString = "123"; // 假設(shè)要檢查的字符串
if (!string.IsNullOrEmpty(myString))
{
// 字符串不是null,也不是空字符串
}
方法2
使用邏輯運算符和string.IsNullOrWhiteSpace方法(如果還要檢查空白字符串,如只包含空格、制表符或換行符的字符串)
string myString ="123"; // 假設(shè)這是要檢查的字符串
if (!string.IsNullOrWhiteSpace(myString))
{
// 字符串不是null,也不是空字符串或僅包含空白字符
}
方法3
使用邏輯運算符和直接比較(只檢查空字符串,不檢查null)
string myString = "123"; // 假設(shè)這是要檢查的字符串
if (myString != null && myString != "")
{
// 字符串不是null,也不是空字符串
}
方法4
使用C# 8.0及更高版本的空合并運算符(null-conditional operator)和邏輯運算符(僅當(dāng)需要提供一個默認(rèn)值時使用)
string myString ="123"; // 假設(shè)這是要檢查的字符串
string nonNullOrEmptyString = myString ?? ""; // 如果myString是null,則nonNullOrEmptyString將被設(shè)置為""
if (nonNullOrEmptyString != "")
{
// 字符串不是空字符串(但可能是null,但在這個例子中已經(jīng)被轉(zhuǎn)換成了"")
}
但是,請注意,上面的方法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"; // 假設(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C#開發(fā)OPC?Server服務(wù)器源碼解析
OPC?Server服務(wù)器服務(wù)器的開發(fā)比較繁瑣,本示例采用C#提供了一種簡單快速實現(xiàn)OPCServer的方法,已經(jīng)在工程項目中應(yīng)用,本文對C#開發(fā)OPC?Server服務(wù)器相關(guān)知識給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
C#使用DeepSeek?API實現(xiàn)自然語言處理,文本分類和情感分析
在C#中使用DeepSeek?API可以實現(xiàn)多種功能,例如自然語言處理、文本分類、情感分析等,本文主要為大家介紹了具體實現(xiàn)步驟,需要的可以了解下2025-02-02
WinForm實現(xiàn)攔截窗體上各個部位的點擊特效實例
這篇文章主要介紹了WinForm實現(xiàn)攔截窗體上各個部位的點擊特效實例,對窗體上各個部位進(jìn)行定義,從而實現(xiàn)了點擊特效,需要的朋友可以參考下2014-09-09
C#實現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)漢字轉(zhuǎn)拼音(支持多音字)的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02
C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)
C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

