字符串是一个字符数组。 String类将文本表示为一系列Unicode字符,并且在.NET基类库中定义。 String类的主要用途是提供属性,运算符和方法,以使使用字符串变得容易。
String类中存在两种类型的运算符:
- Equality(String,String)运算符
- 不等式(String,String)运算符
Equality(String,String)运算符
该运算符用于检查给定的字符串是否包含相同的值。如果两个字符串相等,则返回true。否则,返回false。
句法:
public static bool operator == ( string x, string y );
参数:
string x: String x is the first string to compare.
string y: String y is the second string to compare.
返回值:该运算符的返回类型为System.Boolean 。如果字符串x等于y字符串返回true,否则返回false。
范例1:
CSharp
// C# program to illustrate the
// Equality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
// variables
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
string s3 = "Geeksforgeeks";
bool result1, result2;
// Equality operator return true
// as both strings are equal
result1 = s1 == s2;
Console.WriteLine("s1 is equal to s2: {0} ", result1);
// Equality operator return false
// as both strings are not equal
result2 = s1 == s3;
Console.WriteLine("s1 is equal to s3: {0} ", result2);
}
}
CSharp
// C# program to illustrate
// the Equality operator
using System;
class GFG {
// Main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// Function to check the
// string for equality
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("String 1: {0}", str);
Console.WriteLine("String 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str == value);
}
}
CSharp
// C# program to illustrate the
// Inequality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
// variables
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
string s3 = "Geeksforgeeks";
bool result1, result2;
// Inequality operator return true
// as both strings are different from each other
result1 = s1 != s3;
Console.WriteLine("s1 is different from s3: {0} ", result1);
// Inequality operator return false
// as both strings are equal
result2 = s1 != s2;
Console.WriteLine("s1 is different from s2: {0} ", result2);
}
}
CSharp
// C# program to illustrate the concept
// of Inequality operator
using System;
class GFG {
// main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// method to check the string value
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("string 1: {0}", str);
Console.WriteLine("string 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str != value);
}
}
输出:
s1 is equal to s2: True
s1 is equal to s3: False
范例2:
尖锐的
// C# program to illustrate
// the Equality operator
using System;
class GFG {
// Main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// Function to check the
// string for equality
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("String 1: {0}", str);
Console.WriteLine("String 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str == value);
}
}
输出:
String 1: geeks
String 2: GEEKS
Comparison of string 1 and string 2: False
String 1: geeks
String 2: geeks
Comparison of string 1 and string 2: True
String 1: geeks
String 2: GEEKS
Comparison of string 1 and string 2: False
不等式(字符串,字符串)运算符
该运算符用于检查给定的字符串是否包含不同的值。如果两个字符串彼此不同,则返回true。否则,返回false。
句法:
public static bool operator != ( string x, string y );
参数:
string x: String x is the first string to compare.
string y: String y is the second string to compare.
返回值:该运算符的返回类型为System.Boolean 。如果字符串x不等于y字符串返回true,否则返回false。
下面给出了一些示例,以更好地理解实现:
范例1:
尖锐的
// C# program to illustrate the
// Inequality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
// variables
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
string s3 = "Geeksforgeeks";
bool result1, result2;
// Inequality operator return true
// as both strings are different from each other
result1 = s1 != s3;
Console.WriteLine("s1 is different from s3: {0} ", result1);
// Inequality operator return false
// as both strings are equal
result2 = s1 != s2;
Console.WriteLine("s1 is different from s2: {0} ", result2);
}
}
输出:
s1 is different from s3: True
s1 is different from s2: False
范例2:
尖锐的
// C# program to illustrate the concept
// of Inequality operator
using System;
class GFG {
// main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// method to check the string value
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("string 1: {0}", str);
Console.WriteLine("string 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str != value);
}
}
输出:
string 1: geeks
string 2: GEEKS
Comparison of string 1 and string 2: True
string 1: geeks
string 2: geeks
Comparison of string 1 and string 2: False
string 1: geeks
string 2: GEEKS
Comparison of string 1 and string 2: True
参考: https : //docs.microsoft.com/zh-cn/dotnet/api/system。字符串?view = netframework-4.7.2#运算符