C# 字串運算子


C# 中有兩個字串運算子:等號和不等號。

示例

讓我們看一個等號運算子的示例 −

 線上演示

using System;
public class Demo {
   public static void Main() {
      string str1 = "Amit";
      string str2 = " ";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      Console.WriteLine("Is string1 equal to str2? = "+str1 == str2);
   }
}

輸出

Is string1 null or empty? = False
Is string2 null or empty? = False
False

示例

現在讓我們看一個 C# 不等號運算子的示例 −

 線上演示

using System;
public class Demo {
   public static void Main() {
      string str1 = "abcdef";
      string str2 = "abcdef";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      bool val = str1 != str2;
      Console.WriteLine("Is str1 not equal to str2? = "+val);
   }
}

輸出

Is string1 null or empty? = False
Is string2 null or empty? = False
Is str1 not equal to str2? = False

更新時間: 02-12-2019

499 次瀏覽

開啟你的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.