Test search methods on String - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Test search methods on String

Demo Code

using System;//w  w w  .j  a v a  2  s .co  m
using System.Text;
class Program
{
   static void Main(string[] args)
   {
      string favoriteFood = "cheeseburgers";
      // IndexOf().
      Console.WriteLine("\nIndexOf() method:");
      Console.WriteLine("First s in cheeseburgers at index {0}", favoriteFood.IndexOf('s'));
      // IndexOfAny().
      Console.WriteLine("\nIndexOfAny() method:");
      char[] charsToLookFor = { 'a', 'b', 'c' };
      Console.WriteLine("First a, b, or c at index {0}", favoriteFood.IndexOfAny(charsToLookFor));
      Console.WriteLine("First index of 'a', 'b', or 'c', shorter syntax: {0}", favoriteFood.IndexOfAny(new char[] { 'a', 'b', 'c' }));
      // LastIndexOf().
      Console.WriteLine("\nLastIndexOf() method:");
      Console.WriteLine("Last index of 'b' is {0}", favoriteFood.LastIndexOf('b'));
      // LastIndexOfAny().
      Console.WriteLine("\nLastIndexOfAny() method:");
      Console.WriteLine("Last index of any a, b, or c is {0}", favoriteFood.LastIndexOfAny(charsToLookFor));
      // Contains().
      Console.WriteLine("\nContains() method:");
      Console.WriteLine("Cheeseburgers contains an r: {0}", favoriteFood.Contains("r"));
   }
}

Result


Related Tutorials