Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace LiveDemo
- {
- class SubstringDemo
- {
- public static void Main(string[] args)
- {
- string text = "ABCDEFGHIJKL";
- Console.WriteLine(Substring(text,5));
- Console.WriteLine(text.Substring(5));
- Console.WriteLine("=============================");
- Console.WriteLine(Substring(text,5,1));
- Console.WriteLine(text.Substring(5,1));
- }
- static String Substring(string text, int startIndex, int length)
- {
- if (startIndex < 0 || startIndex > text.Length || length < 0 || startIndex > text.Length - length)
- {
- throw new ArgumentOutOfRangeException();
- }
- if (length == 0)
- {
- return String.Empty;
- }
- if (startIndex == 0 && length == text.Length)
- {
- return text;
- }
- StringBuilder subString = new StringBuilder();
- for (int i = 0; i < length; i++)
- {
- subString.Append(text[startIndex++]);
- }
- return subString.ToString();
- }
- static string Substring(string text, int startIndex)
- {
- return Substring(text, startIndex, text.Length - startIndex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment