String searching 

As the name suggests, string searching involves searching the presence of a particular letter or string in another string. C# provides several methods for doing this. 

Please note that C# is a case-sensitive language. Therefore, searching for a character, let's suppose C, is not the same as searching for the character c in the string.

Please refer to the following different types of searching that are possible with the string object:

  • Contains: When we want to check whether a particular character exists in the string, we use the Contains function. The following example checks whether a character, z, exists in the string object. If it exists, it returns true; otherwise, it returns false.

Let's take a look at the following example:

string s = "hello australia";
var contains = s.Contains("z");
if(contains)
{
Console.WriteLine(" z is present in it.");
}
else
{
Console.WriteLine(" z is not present");
}

In the preceding code, using the Contains function, we are checking whether z occurs in the string against which we are calling the function. As we are calling it for a variable with the value hello australia, it will return the false value as z does not occur in the string. Hence, we get the following output when the code is executed:

 

  • IndexOf: We use this function if we want to find out the index in the string at which a particular character is present.

For example, in the following code example, we are finding the first and the last index of occurrence of the a character in the string hello australia:  

 string s = "hello australia";
var firstIndexOfA = s.IndexOf("a");
Console.WriteLine(firstIndexOfA);
var lastIndexOfA = s.LastIndexOf("a");
Console.WriteLine(lastIndexOfA);

When we execute the program, we will get the first occurrence as 6 and the last occurrence as 14. The IndexOf function retrieves the index the first appearance of a character or a string in the string against which we are using the function. Please also note that it does not ignore spaces. Hence, the whitespace is also counted as a character. Similarly, the LastIndexOf function retrieves the last index of the appearance of the respective character or string:

Please note that in C#, for any array or string, the index of the first character is zero. 
  • StartsWith/EndsWith: We use this function if we want to check whether a string starts or ends with a particular character.

The following code example shows a scenario in which we are checking whether the same string object used previously starts with h and ends with h. In the following code, in the first statement, we are checking whether the s string variable starts with h. Based on the evaluation, we print the output in the console window. Similarly, in the next statement, we are checking whether the same string variable ends with h. Based on the evaluation, we print the output in the console window again:

if(s.StartsWith("h"))
{
Console.WriteLine("It Starts with h.");
}
else
{
Console.WriteLine("It does not starts with h.");
}

if (s.EndsWith("h"))
{
Console.WriteLine("It ends with h.");
}
else
{
Console.WriteLine("It does not ends with h.");
}

Please refer to the following output for the preceding code example:

  • Substring: We use this function if we want to extract a substring from a particular string object. There are two variants of substring possible in C#. In one, we specify just the start index and extract the substring from that particular index. In another variant, we specify both the start and end index and extract the characters present in that substring.

Here is a code example of this:

 string subString = s.Substring(3, 6);
string subString2 = s.Substring(3);
Console.WriteLine(subString);
Console.WriteLine(subString2);

In the preceding code example, we are finding two substrings of a string object, hello australia.

In the first substring, we have passed the start index as 3 and the end index as 6. Therefore, the substring will return us the values, lo aus

In the second substring, we are just passing the start index, 3. Hence, it will return the entire string from this index. The following is the screenshot of the output from this execution:

These are the different string manipulation functions available in C#. In the next section, we will go through an overview of reflection and learn how it helps us to get structure—in other words, classes and their methods and properties—from an assembly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.211.92