ivanov_ivan

Subst

Jun 9th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace LiveDemo
  5. {
  6.     class SubstringDemo
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             string text = "ABCDEFGHIJKL";
  11.             Console.WriteLine(Substring(text,5));
  12.             Console.WriteLine(text.Substring(5));
  13.             Console.WriteLine("=============================");
  14.             Console.WriteLine(Substring(text,5,1));
  15.             Console.WriteLine(text.Substring(5,1));
  16.         }
  17.  
  18.         static String Substring(string text, int startIndex, int length)
  19.         {
  20.             if (startIndex < 0 || startIndex > text.Length || length < 0 || startIndex > text.Length - length)
  21.             {
  22.                 throw new ArgumentOutOfRangeException();
  23.             }
  24.  
  25.             if (length == 0)
  26.             {
  27.                 return String.Empty;
  28.             }
  29.  
  30.             if (startIndex == 0 && length == text.Length)
  31.             {
  32.                 return text;
  33.             }
  34.             StringBuilder subString = new StringBuilder();
  35.             for (int i = 0; i < length; i++)
  36.             {
  37.                 subString.Append(text[startIndex++]);
  38.             }
  39.             return subString.ToString();
  40.         }
  41.  
  42.         static string Substring(string text, int startIndex)
  43.         {
  44.             return Substring(text, startIndex, text.Length - startIndex);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment