设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间20 现在时间20 缓存数据 和聪明人交流,和靠谱的人恋爱,和进取的人共事,和幽默的人随行。晚安!

和聪明人交流,和靠谱的人恋爱,和进取的人共事,和幽默的人随行。晚安!

查看: 2318|回复: 3

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

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:205
  • 打卡月天数:0
  • 打卡总奖励:3193
  • 最近打卡:2023-08-27 07:15:46
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
415
主题
396
精华
0
金钱
4440
积分
839
注册时间
2022-12-20
最后登录
2025-9-11

发表于 2024-7-5 19:58:54 | 显示全部楼层 |阅读模式
目录


  • 方法1
  • 方法2
  • 方法3
  • 方法4
  • 测试代码

方法1

使用逻辑运算符和string.IsNullOrEmpty方法
  1. string myString = "123"; // 假设要检查的字符串  
  2. if (!string.IsNullOrEmpty(myString))  
  3. {  
  4.     // 字符串不是null,也不是空字符串  
  5. }
复制代码
方法2

使用逻辑运算符和string.IsNullOrWhiteSpace方法(如果还要检查空白字符串,如只包含空格、制表符或换行符的字符串)
  1. string myString ="123"; // 假设这是要检查的字符串  
  2. if (!string.IsNullOrWhiteSpace(myString))  
  3. {  
  4.     // 字符串不是null,也不是空字符串或仅包含空白字符  
  5. }
复制代码
方法3

使用逻辑运算符和直接比较(只检查空字符串,不检查null)
  1. string myString = "123"; // 假设这是要检查的字符串  
  2. if (myString != null && myString != "")  
  3. {  
  4.     // 字符串不是null,也不是空字符串  
  5. }
复制代码
方法4

使用C# 8.0及更高版本的空合并运算符(null-conditional operator)和逻辑运算符(仅当需要提供一个默认值时使用)
  1. string myString ="123"; // 假设这是要检查的字符串  
  2. string nonNullOrEmptyString = myString ?? ""; // 如果myString是null,则nonNullOrEmptyString将被设置为""  
  3. if (nonNullOrEmptyString != "")  
  4. {  
  5.     // 字符串不是空字符串(但可能是null,但在这个例子中已经被转换成了"")  
  6. }
复制代码
但是,请注意,上面的方法4只检查了空字符串,并没有检查原始字符串是否为null。如果需要同时检查null和空字符串,最好使用第一种或第二种方法。

测试代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         private void strFun1()
  18.         {
  19.             string myString ="123"; // 假设要检查的字符串  
  20.             if (!string.IsNullOrEmpty(myString))
  21.             {
  22.                 // 字符串不是null,也不是空字符串  
  23.                 MessageBox.Show("字符串不是null,也不是空字符串");
  24.             }
  25.             myString = null;
  26.             if (string.IsNullOrEmpty(myString))
  27.             {
  28.                 // 字符串不是null,也不是空字符串  
  29.                 MessageBox.Show("字符串是null或是空字符串");
  30.             }
  31.             myString = "";
  32.             if (string.IsNullOrEmpty(myString))
  33.             {
  34.                 // 字符串不是null,也不是空字符串  
  35.                 MessageBox.Show("字符串是null或是空字符串");
  36.             }
  37.         }

  38.         private void strFun2()
  39.         {
  40.             string myString ="123"; // 假设这是要检查的字符串  
  41.             if (!string.IsNullOrWhiteSpace(myString))
  42.             {
  43.                 // 字符串不是null,也不是空字符串或仅包含空白字符  
  44.                 MessageBox.Show("字符串不是null,也不是空字符串或仅包含空白字符");
  45.             }
  46.             myString = null;
  47.             if (string.IsNullOrWhiteSpace(myString))
  48.             {
  49.                 // 字符串不是null,也不是空字符串  
  50.                 MessageBox.Show("字符串是null或是空字符串或仅包含空白字符");
  51.             }
  52.             myString = "";
  53.             if (string.IsNullOrWhiteSpace(myString))
  54.             {
  55.                 // 字符串不是null,也不是空字符串  
  56.                 MessageBox.Show("字符串是null或是空字符串或仅包含空白字符");
  57.             }
  58.             myString = "  ";
  59.             if (string.IsNullOrWhiteSpace(myString))
  60.             {
  61.                 // 字符串不是null,也不是空字符串  
  62.                 MessageBox.Show("字符串是null或是空字符串或仅包含空白字符");
  63.             }
  64.         }
  65.         private void strFun3()
  66.         {
  67.             string myString = "123"; // 假设要检查的字符串  
  68.             if (myString != null && myString != "")
  69.             {
  70.                 // 字符串不是null,也不是空字符串  
  71.                 MessageBox.Show("字符串不是null,也不是空字符串");
  72.             }
  73.             myString = null;
  74.             if (myString == null )
  75.             {
  76.                 // 字符串是null
  77.                 MessageBox.Show("字符串是null");
  78.             }
  79.             myString = "";
  80.             if (myString == "")
  81.             {
  82.                 // 字符串是空字符串  
  83.                 MessageBox.Show("字符串是空字符串");
  84.             }
  85.         }

  86.         private void strFun4()
  87.         {
  88.             string myString = "123"; // 假设要检查的字符串  

  89.             string nonNullOrEmptyString = myString ?? ""; // 如果myString是null,则nonNullOrEmptyString将被设置为""  


  90.             if (nonNullOrEmptyString != null && nonNullOrEmptyString != "")
  91.             {
  92.                 // 字符串不是null,也不是空字符串  
  93.                 MessageBox.Show("字符串不是null,也不是空字符串");
  94.             }
  95.             if (nonNullOrEmptyString == null)
  96.             {
  97.                 // 字符串是null
  98.                 MessageBox.Show("字符串是null");
  99.             }
  100.             if (nonNullOrEmptyString == "")
  101.             {
  102.                 // 字符串是空字符串  
  103.                 MessageBox.Show("字符串是空字符串");
  104.             }
  105.         }

  106.         private void button1_Click(object sender, EventArgs e)
  107.         {
  108.             strFun1();
  109.         }

  110.         private void button2_Click(object sender, EventArgs e)
  111.         {
  112.             strFun2();
  113.         }

  114.         private void button3_Click(object sender, EventArgs e)
  115.         {
  116.             strFun3();
  117.         }

  118.         private void button4_Click(object sender, EventArgs e)
  119.         {
  120.             strFun4();
  121.         }
  122.     }
  123. }
复制代码
1.png

2.png

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
11
积分
2
注册时间
2023-7-8
最后登录
2023-7-8

发表于 2024-9-12 16:06:12 | 显示全部楼层
感谢楼主,顶。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
14
积分
8
注册时间
2022-12-26
最后登录
2022-12-26

发表于 2024-12-5 18:30:58 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-26
最后登录
2022-12-26

发表于 2025-10-3 00:08:11 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼
4楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表