How to use IndexOf Substring and Reverse in c# with examples

Here I would like to explain IndexOf and Substring and Reverse and How to use those functions in c#.

 

IndexOf  with Example: 

 

This is used for finding the index of a character and testing the location of a specified string.

 

string Name="Sathish";

IndexOf  is case sensitive method. example here name is sathish I want know the index for the character h
 

Console.Writeline(Name.IndexOf('H'));

It will print -1. Name.IndexOf('H')  will return -1-----> becuase it is a case sensitive method.

Console.Writeline(Name.IndexOf('h'))---> It will return 3. this method returns the index of first occurence of a character.

 

SubString with Example


The name it self tells that it is sub string of a string.

We can pass one argument and we can also pass two arguments.

One argument  : in this we pass only starting index, then this method return remaining from the starting index.

Two argument  : in this case we pass two areguments one is indicating the starting position the string and second parameter indecates the end of the substring.

 

  Reverse of a String and how to convert string to Char array

we are converting a string to char array as 
char[] arr=string.ToCharArray();
you can not print Console.Writeline(Array.Reverse(arr)), it will thows an error. we need to update the character array first then we have to print

1 comment: