Demonstrating StartsWith and EndsWith methods. - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Demonstrating StartsWith and EndsWith methods.

Demo Code



using System;//from  ww w . ja v  a2s.  c  o m

class StringStartEnd
{
   static void Main()
   {
      string[] strings = { "started", "starting", "ended", "ending" };

      foreach (var element in strings)
      {
         if (element.StartsWith("st"))
         {
            Console.WriteLine($"\"{element}\" starts with \"st\"");
         }
      }

      Console.WriteLine();

      // test every string to see if it ends with "ed"
      foreach (var element in strings)
      {
         if (element.EndsWith("ed"))
         {
            Console.WriteLine($"\"{element}\" ends with \"ed\"");
         }
      }
      Console.WriteLine();
   }
}

Result


Related Tutorials