C#String.Equality运算符 (C# String.Equality operator )
"==" is a String.Equality operator in C#, it is used to check whether two strings objects have the same values or not.
“ ==”是C#中的String.Equality运算符 ,用于检查两个字符串对象是否具有相同的值。
Syntax:
句法:
public static bool operator == (string a, string b);
Parameter: It has two parameters both are strings to compare..
参数:它有两个参数都是要比较的字符串。
Return value: bool – it returns a Boolean value. If strings have the same value, it returns true, else it returns false.
返回值: bool-返回布尔值。 如果字符串具有相同的值,则返回true ,否则返回false 。
Example:
例:
Input:
string str1 = "IncludeHelp";
string str2 = "IncludeHelp";
String.Equality:
str1 == str2;
Output:
true
C#使用String.Equality(==)运算符比较两个字符串的示例 (C# Example to compare two strings using String.Equality (==) operator )
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str1 = "IncludeHelp";
string str2 = "IncludeHelp";
//comparing strings
Console.WriteLine("str1==str2: " + (str1 == str2));
if (str1 == str2)
Console.WriteLine("str1 and str2 have the same values");
else
Console.WriteLine("str1 and str2 don't have the same values");
str1 = "Hello world";
str2 = "IncludeHelp";
//comparing strings
Console.WriteLine("str1==str2: " + (str1 == str2));
if (str1 == str2)
Console.WriteLine("str1 and str2 have the same values");
else
Console.WriteLine("str1 and str2 don't have the same values");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
str1==str2: True
str1 and str2 have the same values
str1==str2: False
str1 and str2 don't have the same values
Reference: String.Equality(String, String) Operator
参考: String.Equality(String,String)运算符
翻译自: https://www.includehelp.com/dot-net/string-equality-operator-with-example-in-c-sharp.aspx