Q:

Write a C# Sharp program to create a new string of length 2 starting at the given index of a given string

0

Write a C# Sharp program to create a new string of length 2 starting at the given index of a given string

Sample Output:

el
th
on

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {
            Console.WriteLine(test("Hello", 1));
            Console.WriteLine(test("Python", 2));
            Console.WriteLine(test("on", 1));
            Console.ReadLine();
        }        
      public static string test(string s1, int index)
        {
            return index + 2 <= s1.Length ? s1.Substring(index, 2) : s1.Substring(0, 2);
        }
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now