VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-string-operators/

⇱ C# | String Operators - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | String Operators

Last Updated : 11 Jul, 2025

The string is an array of characters. The String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of the String class is to provide the properties, operators and methods so that it becomes easy to work with strings.
There are two types of operators present in the String class: 
 

  1. Equality(String, String) Operator
  2. Inequality(String, String) Operator


 

Equality(String, String) Operator


This operator is used to check whether the given string contains the same value or not. It returns true if both the string are equal. Otherwise, return false.
Syntax: 
 

public static bool operator == ( string x, string y );


Parameters:
 

string x: String x is the first string to compare. 
string y: String y is the second string to compare. 
 


Return value: The return type of this operator is System.Boolean. It returns true if string x is equal to string y, otherwise return false.
Example 1:
 

Output: 
 

s1 is equal to s2: True 
s1 is equal to s3: False 


Example 2:
 

Output: 
 

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


 

Inequality(string, string) Operator


This operator is used to check whether the given strings contain different values or not. It returns true if both the strings are different from each other. Otherwise, return false.
Syntax: 
 

public static bool operator != ( string x, string y );


Parameters:
 

string x: String x is the first string to compare. 
string y: String y is the second string to compare. 
 


Return Value: The return type of this operator is System.Boolean. It returns true if string x is not equal to string y, otherwise return false.
Below given are some examples to understand the implementation in a better way: 
Example 1:
 

Output:
 

s1 is different from s3: True 
s1 is different from s2: False


Example 2:
 

Output: 
 

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


Reference: https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2#operators
 

Comment
Article Tags:
Article Tags:

Explore