c sharp

C# String Compare

“A string is a data variable that contains several characters to form a collective string. The variable string is declared as an object of the string and used to inhabit several features used in the string class. This tutorial will highlight the comparison methods in C sharp programming language.”

Example 1

In this example, we will use a built-in function of C sharp to compare two strings. As string class in C sharp is responsible for applying different features. So “string.Equals” is one of the functions in C sharp that is used to compare strings in the .Net framework. To elaborate on the concept, we have used the program’s source code. We have taken three strings. These three strings will be compared with one another by using the string.Equals function.

This function takes two parameters simultaneously; not all three strings will be compared at once. Whereas the return type of this function is a boolean. It returns either true or false values depending on the condition applied in the program.

# String.Equals(string1 string2);

As it is a comparison method, we will use an if-else statement to display the resultant values. If both the first two strings are the same, then the function will return True, so the first statement will be displayed that both strings are the same; otherwise, in the case of a False return, it will be displayed that both are different values.

# If(String.Equals (mystring1, mystring2))

# Console.Writeline($"{mystring1} and mystring2} are same");

Both the values inside the variables will be displayed in the resultant statement.

A similar if-else statement will be applied for the other two comparisons between string 1 and string 3 and the comparison between string 2 and string 3. As input strings are taken so that the first two strings are the same while the third one is different, the results will be seen on execution.

To execute any source code in C sharp, we need to use a compiler to compile the code for the Ubuntu terminal. So here, we have used the MCS compiler. Afterward, the code is compiled; there is a need to execute the code in the file with the .exe extension. Mono is recommended for this purpose.

$ Mcs file.cs

$ Mono file.exe

Example 2

The second method used for string comparison is a string.Compare() method. String.Compare is also part of the String class in C sharp. This function also takes two strings as a parameter.

# String.Compare(string1, string2);

Irrespective of the first example, this function contains the return type as an integer value. Both the strings are compared. In case the first string is lexicographically less as compared to the second one, then the return value is less than zero. And if the second string is smaller in terms lexicographically, the value is greater than zero. And it returns zero if both are similar.

Hint: Lexicographical is a term of the order. Lexicographic order is a generalized form of alphabetical order. In other words, it is said to be a dictionary of a sequence of order symbols.

Three strings and one integer type data variable are declared. The integer data variable is used to store the resultant value in it. The string values are taken so that the first two strings are the same, while the third one is different.

# Result = string.Compare(str1 , str2);

# Console.writeline (result);

The same function is applied to all comparisons between these three strings.

From the results, you will see that all three different values are obtained. The first one is zero, which means both strings are the same; the second result is -1, which means that the second string is greater than the first, whereas 1 shows that the second string is smaller than the first one.

Example 3

The third method in use is the “CompareTo()” method. This function is a direct comparison method because it takes a single string in the parameter. This function is called through the object of string, and to whom it is compared is written inside the parameter of the function.

# String1. CompareTo(String2);

In this illustration, we have taken two different strings. We use an if-else statement to compare the returned value of two strings. This function also returns the integer values. And the criteria for the return data are the same as described for the string.compare() method. When the program is executed, both the strings will be compared with each other. When the compiler goes towards the if statement, according to that, if the result of the comparison is zero, then display statement as both the strings are different so it will not true, the result is minus 1, so according to this situation, the else part will be executed, and the statement will be displayed to console.

On the execution, you can see that it is stated that red is smaller than yellow.

Example 4

This example includes a comparison by character by character. This is also known as a custom comparison as we declare this function by ourselves; this is not a built-in function of a string. At a time, this also comprises a comparison of the two strings. First, we declare a method that is static outside the main function to compare two strings.

Now move towards the code to elaborate the working of this phenomenon in C sharp. A function that is declared for the comparison purpose will take two strings as an argument. Inside the function, a long integer type variable is declared to store the minimum of both string lengths. This is done through the built-in mathematics function Math.min().

# int len = Math.Min(myString1.Length, myString2.Length);

After that, we will use the “For” loop to iterate through the whole string character by character comparison. The loop will iterate till the last character of both the strings is compared. We have utilized the if statement to check two strings and compare them with each other.

# If (Mystring1[index] < mystring2[index])

And similarly, if that character of the first string is greater than the second string’s first character, then return 1, and zero is returned if both are equal.

In the end, the lengths of both strings are compared. If the second string is greater in length than the first one, then it returns 1; on the other hand, -1 is returned.

In the main program, two strings are declared. A function call is made, and the returned value is stored in the variable.


Comparison of strings is made through different approaches in C sharp programming language. We have implemented all the approaches in the Linux operating system. All methods are built-in functions of Strings, except the one that includes the user-defined function manually to compare two strings. String.compare(), String.equal(), and compareTo() are the basic functions of strings that assist in comparing two strings with each other by using strings as arguments. This tutorial will provide you with all the possible methodologies by using some elementary examples.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.