“!=”是C#中的String.Inequality运算符,用于检查两个字符串对象是否具有相同的值(如果字符串不具有相同的值,则返回true)。
语法:
public static bool operator != (string a, string b);
参数:它有两个参数都是要比较的字符串。
返回值: bool-返回布尔值。如果字符串具有相同的值,则返回false,否则返回true。
示例
Input: string str1 = "nhooo"; string str2 = "nhooo"; String.Inequality: str1 != str2; Output: false
using System; using System.Text; namespace Test { class Program { static void Main(string[] args) { string str1 = "nhooo"; string str2 = "nhooo"; //比较字符串 Console.WriteLine("str1!=str2: " + (str1 != str2)); if (str1 != str2) Console.WriteLine("str1 and str2 don't have the same values"); else Console.WriteLine("str1 and str2 have the same values"); str1 = "Hello world"; str2 = "nhooo"; //比较字符串 Console.WriteLine("str1!=str2: " + (str1 != str2)); if (str1 != str2) Console.WriteLine("str1 and str2 don't have the same values"); else Console.WriteLine("str1 and str2 have the same values"); //按ENTER退出 Console.ReadLine(); } } }
输出结果
str1!=str2: False str1 and str2 have the same values str1!=str2: True str1 and str2 don't have the same values
参考:运算符String.Inequality(String, String)