String Comparison and Searching

In C#, string comparison and searching are common operations used to determine if one string is equal to, comes before, or comes after another string, as well as to locate substrings within a larger string. C# provides several methods for performing these tasks. Here are some of the key methods:

1. String Comparison:
•'string.Equals': The 'Equals' method compares two strings for equality and returns a 'Boolean' value indicating whether they are equal or not.


string str1 = "Hello";
string str2 = "hello";

bool areEqual = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(areEqual); // Output: True (ignoring case)

•'string.Compare': The 'Compare' method compares two strings and returns an integer that indicates their relative position in lexicographic order.


string str1 = "apple";
string str2 = "banana";

int result = string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase);

if (result < 0)
    Console.WriteLine("str1 comes before str2.");
else if (result > 0)
    Console.WriteLine("str1 comes after str2.");
else
    Console.WriteLine("str1 and str2 are equal.");

2. String Searching:
•'string.Contains': The 'Contains' method checks if a substring exists within a string and returns a 'Boolean' value indicating the result.


string sentence = "The quick brown fox jumps over the lazy dog.";

bool containsFox = sentence.Contains("fox");
Console.WriteLine(containsFox); // Output: True

•'string.IndexOf': The 'IndexOf' method returns the index of the first occurrence of a substring within the string. If the substring is not found, it returns -1.


string sentence = "The quick brown fox jumps over the lazy dog.";

int index = sentence.IndexOf("fox");
Console.WriteLine(index); // Output: 16

•'string.LastIndexOf': The 'LastIndexOf' method is similar to 'IndexOf' but searches for the last occurrence of the substring within the string.


string sentence = "The quick brown fox jumps over the lazy dog.";

int lastIndex = sentence.LastIndexOf("o");
Console.WriteLine(lastIndex); // Output: 40

•'string.StartsWith' and 'string.EndsWith': These methods check if a string starts or ends with a specific substring and return a 'Boolean' value.


string fileName = "example.txt";

bool startsWithEx = fileName.StartsWith("ex");
Console.WriteLine(startsWithEx); // Output: True

bool endsWithTxt = fileName.EndsWith(".txt");
Console.WriteLine(endsWithTxt); // Output: True

•'string.Substring': The 'Substring' method extracts a portion of a string based on a start index and optional length.


string sentence = "The quick brown fox jumps over the lazy dog.";

string subString = sentence.Substring(16, 3);
Console.WriteLine(subString); // Output: "fox"

These methods allow you to perform various string comparison and searching tasks in C#. Depending on your specific use case, you can choose the appropriate method to determine if strings are equal, compare their positions, or locate substrings within larger strings.