C# 字符串运算符
csharpserver side programmingprogramming更新于 2025/9/15 12:30:00
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("string2 是否为 null 或为空?="+string.IsNullOrEmpty(str2));
Console.WriteLine("string1 是否等于 str2?="+str1 == str2);
}
}
输出
string1 是否为 null 或为空?= False string2 是否为 null 或为空?= 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

