Substring User Defined

Pranat
0
Lamp

 using System;

class ServerTest

{

 public string str = "";

 public ServerTest()

 {

 Console.Write("Enter Your String: ");

 str = Console.ReadLine();

 //Default Substring() Functions

 Console.WriteLine(str.Substring(3));

 Console.WriteLine(str.Substring(3,8));

 }

 public string SubString1(int startIndex)

 {

 string substr = "";

 for (int i = startIndex; i < str.Length; i++)

 {

 substr = substr + str[startIndex];

 startIndex++;

 }

 return substr;

 }

 public string SubString1(int startIndex, int length)

 {

 string substr = "";

 if (length > str.Length)

 {

 Console.WriteLine("Invalid Input IndexOutOfRange!");

 }

 else

 {

 for (int i = startIndex; i <= length + 2; i++) //DOUBT: Why It need length+2

 {

 substr = substr + str[startIndex];

 startIndex++;

 }

 }

 return substr;

 }

}

class ClientTest

{

 public static void Main()

 {

 ServerTest s = new ServerTest();


Console.WriteLine("------------------------------------");

 Console.WriteLine(s.SubString1(3));

 Console.WriteLine(s.SubString1(3, 8));

 Console.WriteLine("------------------------------------");

 //Pramod Vishnu Naik

 }

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !