Finding Substrings in C# Programming

Using C# Programming in easy steps

The C# String class library provides a number of methods that can be dot-suffixed to a string variable to seek a string within a string (i.e. a “substring”). The IndexOf( ) method requires the substring as its argument and, if found, returns the index position of the substring within the searched string. Otherwise it will return -1. The IndexOf( ) method searches forwards, from left-to-right, and returns the index position of the first occurence of the substring. It has a companion LastIndexOf( ) method that works in just the same way, but searches backwards, from right-to-left.

Similarly, there is an IndexOfAny( ) method and its companion LastIndexOfAny( ) method that require a character array argument. These seek any character of the specified array within a string, and return the index position of the first occurrence, or -1 otherwise.

You can also dot-suffix a Substring( ) method to a string variable if you want to extract a copy of a substring from within that string. This method requires two arguments to specify the index position at which to begin copying, and the number of characters to copy:



The position reported when a substring is found is the index position of the first character of that substring within the searched string – whether searching forwards or backwards.




Step 1
Start a new Console Application, then name the project and Console.Title as “Substring”

Step 2
After the Main( ) method, add this method declaration to report the result of a substring search
static void report( int pos , string sub )
if( pos != -1 )
{ Console.WriteLine( “‘“ + sub + ”’ Found At “ + pos ) ; }
{ Console.WriteLine( “‘“ + sub + ”’ Not Found!“ ) ; }

Step 3
Now, turn your attention to the Main( ) method and initialize a string variable, then display its value and length
string text = “My dog is a cute dog” ;
Console.WriteLine( text + ”\tLength: “ + text.Length ) ;

Step 4
Next, request user input to initialize another string variable with a substring to seek
Console.WriteLine( “\nPlease Enter A Substring To Seek: ” ) ;
string sub = Console.ReadLine( ) ;
Step 5
Create a character array and copy the entire substring value into the character array
char [ ] arr = new char[ sub.Length ] ;
sub.CopyTo( 0 , arr , 0 , sub.Length ) ;

The CopyTo( ) method is described and demonstrated in the previous example on pages 76-77.

Step 6
Then, seek the first occurrence of the substring and call the method you defined to report the search result
int pos = text.IndexOf( sub ) ;
report( pos , sub ) ;

Step 7
Now, seek the last occurrence of the substring and report the result
pos = text.LastIndexOf( sub ) ;
report( pos , sub ) ;

Step 8
Next, seek the first occurrence of any character of the substring and report the result
pos = text.IndexOfAny( arr ) ;
report( pos , text.Substring( pos , 1 ) ) ;



Notice that a copy of the located character is extracted from the original string using the Substring( ) method for output in the report.



Step 9
Finally, seek the last occurrence of any character of the substring and report the result
pos = text.LastIndexOfAny( arr ) ;
report( pos , text.Substring( pos , 1 ) ) ;
Console.ReadKey( ) ;

Step 10
Press Start or F5 to run the application, then enter a substring to seek and see the reported results






Want to know more?

For the complete C# Programming guide, all in the trusted In Easy Steps style, click here. In full-colour and straightforward, jargon-free language, C# Programming in Easy Steps will teach you to code applications, and demonstrates every aspect of the C# language you will need to produce professional programming results. Its examples provide clear syntax-highlighted code showing C# language basics including variables, arrays, logic, looping, methods, and classes.