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
// 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);
}
}
Output:
s1 is equal to s2: True
s1 is equal to s3: False
Example 2:
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);
}
}
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
// 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);
}
}
Output:
s1 is different from s3: True
s1 is different from s2: False
Example 2:
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);
}
}
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 Class
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 represented by a class System.String. The String c
9 min read
C++ | Nested Ternary Operator
Ternary operator also known as conditional operator uses three operands to perform operation. Syntax : op1 ? op2 : op3; Nested Ternary operator: Ternary operator can be nested. A nested ternary operator can have many forms like : a ? b : ca ? b: c ? d : e ? f : g ? h : ia ? b ? c : d : e Let us unde
5 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
C String Functions
C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th
6 min read
Perl | Useful String Operators
A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (â) or double quote (â). Operators a
3 min read
Operators in C
In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.What is an Operator in C?A C operator can be defined as the symbol that helps us
11 min read
How to input or read a Character, Word and a Sentence from user in C?
C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write an operating system. The main features of the C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suit
4 min read
Lexical Analyser in C
In C, the lexical analysis phase is the first phase of the compilation process. In this step, the lexical analyzer (also known as the lexer) breaks the code into tokens, which are the smallest individual units in terms of programming. In the lexical analysis phase, we parse the input string, removin
6 min read
Unary Operators in C
In C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in
5 min read
How to Take Operator as Input in C?
In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we
2 min read