How to Get Last Index of String

Here, we use IndexOf to see if a string contains a word. We test the string for a substring "Man" We test the result of IndexOf against the special constant -1.

Example:

IndexOf returns the location of the string "Man" It is not equal to -1. So the line is written to the console window.

Usually we want to know the exact result of IndexOf. We can store its result in an int local.

Based on: .NET 4.5 C# program that uses IndexOf

  1. using System;  
  2. class Program1  
  3. {  
  4.     static void Main()  
  5.     {  
  6.         // The input string.   
  7.         const string value = "Man is cute.";  
  8.         // Test with IndexOf method.   
  9.         if (value.IndexOf("Man") != -1)  
  10.         {  
  11.             Console.WriteLine("string contains Man!");  
  12.         }  
  13.     }  
  14. }  
Output:

string contains Man!