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:
- Equality(String, String) Operator
- 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:
CSharp
using System;
class GFG {
public static void Main( string [] args)
{
string s1 = "WelcomeToGeeks" ;
string s2 = "WelcomeToGeeks" ;
string s3 = "Geeksforgeeks" ;
bool result1, result2;
result1 = s1 == s2;
Console.WriteLine( "s1 is equal to s2: {0} " , result1);
result2 = s1 == s3;
Console.WriteLine( "s1 is equal to s3: {0} " , result2);
}
}
|
Output:
s1 is equal to s2: True
s1 is equal to s3: False
Example 2:
CSharp
using System;
class GFG {
public static void Main()
{
Check( "GEEKS" );
Check( "geeks" );
Check( "GEEKS" );
}
static void Check(String value)
{
String str = "geeks" ;
Console.WriteLine( "String 1: {0}" , str);
Console.WriteLine( "String 2: {0}" , value);
Console.WriteLine( "Comparison of string 1 and string 2: {0}" ,
str == value);
}
}
|
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:
CSharp
using System;
class GFG {
public static void Main( string [] args)
{
string s1 = "WelcomeToGeeks" ;
string s2 = "WelcomeToGeeks" ;
string s3 = "Geeksforgeeks" ;
bool result1, result2;
result1 = s1 != s3;
Console.WriteLine( "s1 is different from s3: {0} " , result1);
result2 = s1 != s2;
Console.WriteLine( "s1 is different from s2: {0} " , result2);
}
}
|
Output:
s1 is different from s3: True
s1 is different from s2: False
Example 2:
CSharp
using System;
class GFG {
public static void Main()
{
Check( "GEEKS" );
Check( "geeks" );
Check( "GEEKS" );
}
static void Check(String value)
{
String str = "geeks" ;
Console.WriteLine( "string 1: {0}" , str);
Console.WriteLine( "string 2: {0}" , value);
Console.WriteLine( "Comparison of string 1 and string 2: {0}" ,
str != value);
}
}
|
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://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2#operators
Similar Reads
C# String Properties
In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the string class: Chars[Int32]: Used to get the Char object at a spe
4 min read
C# Operators
In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result. Types of Opera
8 min read
C# | Operator Overloading
Prerequisite: Operators in C# The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. It ena
4 min read
C# Strings
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is an important concept, and sometimes people get con
8 min read
Reverse String in C
In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C. The most straightforward method to reverse string is by using two pointers
3 min read
C/C++ Program for String Search
C/C++ Program for Naive Pattern SearchingC/C++ Program for KMP AlgorithmC/C++ Program for Rabin-Karp AlgorithmC/C++ Program for A Naive Pattern Searching QuestionC/C++ Program for Finite AutomataC/C++ Program for Efficient Construction of Finite AutomataC/C++ Program for Boyer Moore Algorithm â Bad
1 min read
How to Reverse a String in C?
In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu
2 min read
Array of Pointers to Strings in C
In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
2 min read
C Program to Sort a String of Characters
Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program. The most straightforward method to sort a string of characters is by using the qsort() function. Let's tak
3 min read
How to Convert an Integer to a String in C?
Write a C program to convert the given integer value to string. Examples Input: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234". Input: -567Output: "-567"Explanation: The integer -567 is converted to the string "-567". Different Methods to Convert an Integer to a St
4 min read