Search This Blog

String IndexOf method c#

Posted by Team on
we use IndexOf simply to see whether the input string contains a string. We want to see if the string in the example contains "lucky".
Program that uses IndexOf [C#]
using System;

class Program
    static void Main()
 // A.
 // The input string.
 const string s = "I am lucky.";

 // B.
 // Test with IndexOf.
 if (s.IndexOf("lucky") != -1)
     Console.Write("string contains 'lucky'");
Output string contains 'lucky' Description. In part A, it has an input string. This string is what we want to test. In part B, it calls IndexOf. IndexOf returns the location of the string 'lucky'. It is not equal to -1, so the line is written to the console window.