c sharp

C# IndexOf

While programming, we tend to use variables of different kinds. One of those kinds is “string” which is used to store many characters, substrings, numbers and special characters in it. In C#, we are provided with the “IndexOf()” function to search for the index of the first occurrence for the specific character or substring from its original string. The IndexOf() function of C# returns “-1” when a specific value cannot be found in any of the indexes of a variable. Otherwise, it will return the index of a value to be searched. Therefore, we have decided to explore the uses of the “IndexOf()” function in the C# code today. For this, make sure to have C# libraries installed. Starting from it, use the “touch” query for the C# file creation and name it “indexof.cs”.

Example 01

We will be starting from the basic one and illustrate the use of the IndexOf function on a string variable to search the particular index number of a character from it. So, we have been starting this example tutorial with the “System” library of our system. It is a must-use library in a C# program so that we can use the generic member function in the code. Started this program implementation from the declaration of a simple Test class and the static main() method initialization.

Within this main() method, we have a string variable “s” with a string value. The C#’s WriteLine() function from the Console class of the System library is used to display the string first. After this, an integer variable “i1” has been declared to get the index of character “I” from the string variable “s” using the IndexOf() function of C#. The next Console.WriteLine() statement is here to display that index using the “i1” variable value. The same process has been used for the character “n” and got its index in the variable “i2” from the string “s”. This is all about finding the index number of a single character from the string variable. Save this code.

After saving the code, we are back to the terminal. In Ubuntu, you must have the “mcs” compiler and “mono” runtime for C# installed at your end. So, we have been trying the “mcs” compiler to compile the code file “indexof.cs”. Its “exe” file will be created, and we have to use the “mono” runtime to run that “exe” file for execution as we did below. The output area shows the display of string “s” first and then the index of “I” and “n” on separate lines. The result shows that the IndexOf function returns the first-most occurrence index from the string.

Example 02

In our above illustration, we have demonstrated a way to get the character’s first most index occurrence from the string. Now, we have a unique way to define the starting point from which we will be searching for the matching character. So, we have been updating the same code. The same string “s” has been used with the same value. We have updated the arguments of the “IndexOf” function for each integer variable “i1” and “i2”. We have been searching the first occurrence for the same characters “I” and “n” while stating the starting point index from the string. For “I”, the IndexOf() function will search from onward index number 4, and for “n” it will search onward from the index “5”. Make sure to use the last occurrence “index” number as a starting point while using this method. So, the updated code has to be saved first before execution.

After trying the compilation on the “indexof.cs” file, we have tried its “exe” file for execution using the “mono” runtime. The below-shown updated output is now presented on our screen. It shows the index for the first occurrences for both characters “I” and “n” after the starting points.

Example 03

You can also search for the first occurrences of characters from the string by defining the starting and ending points both. Let’s suppose you want to search for the occurrence of a character at some last indexes of a string. For this, you need to define the starting and ending point index from your string within the IndexOf function. We have been updating the code to show this.

So, updating the IndexOf() function arguments, we have been using the starting point 4 and ending point 15 for the character “I” to be searched from the string “s”. This means that only the indexes from 4 to 15 will be searched throughout for the character “I” first occurrence. For the character “n”, we have been defining the starting point 12 for its second last occurrence. As the last occurrence, we want to search is at the last index of a string “s”, there is no need to define the last index number as an ending point because it will throw an error. Now, the updated code will be saved for execution.

After compiling and executing our code, we have found the first occurrence of “I” at index “6” starting 4 to 15. While the first occurrence of “n” has been found at index “13” of string “s” starting from point 12.

If you use the ending point index as the last occurrence index of a specific character from the string, it will throw an “ArgumentOutofRangeException” Exception on your screen after running the “exe” file as shown below. So, avoid doing that.

Example 04

Let’s check how the IndexOf() function responds to the use of different case characters in its arguments. So, we have been trying the updated code as displayed below. The string “s” has been initialized with a new string value, as shown. We have been searching for the index of character “f” from the string “s”.

We are using the upper-case and lower-case representation of a character to search it from the string “s”. Both the searches for indexes of “f” and “F” characters have been done on separate lines using the “IndexOf” function.

The result of running this code shows the output “6” for ‘”f” and “-1” for “F” because the uppercase “F” has not been found as the “IndexOf” function performs a case-sensitive search.

Example 05

The “IndexOf()” function can also be utilized for the first index search of a substring from the string. Let’s see an illustration for this too. So, we have initialized a string “s” with a long string value and displayed it using the Console.WriteLine(). After this, we are using the “IndexOf()” function to search for the first occurrences of substring “string” and “Hello” from the string “s”.

After the compilation and execution of this updated code, we have found the first occurrence of substring “string” at index 10 and didn’t found the occurrence of “Hello” so far.

Conclusion

This demonstration was all about the index search for a particular pattern from the given string using the IndexOf() function of C#. We have discussed its use and output in the introductory paragraph to avoid any misconceptions. Along with that, we have tried many of its examples using C# codes to discuss its use for simple characters or a substring. Also, we have tried to use the starting point and ending point indexes within the IndexOf function to search for the first occurrence of a specific character or a substring.

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.