String IndexOf method c#

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'");
}
Console.ReadLine();
}
}

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.

Advertisements

Say something : I accept all the "Humer&Critic"

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s