Advertisement
Guest User

Split string with delimiters

a guest
Sep 24th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SplitWithDelimiter
  5. {
  6.   internal static class Program
  7.   {
  8.     internal static void Main()
  9.     {
  10.       const bool addEmptyStrings = false;
  11.       Display(MySplit1("Hello****wide****world", "****", addEmptyStrings));
  12.       Display(MySplit1("Hello****wide****world****", "****", addEmptyStrings));
  13.       Display(MySplit1("****Hello****wide********world", "****", addEmptyStrings));
  14.  
  15.       Display(MySplit2("Hello****wide****world", "****", addEmptyStrings));
  16.       Display(MySplit2("Hello****wide****world****", "****", addEmptyStrings));
  17.       Display(MySplit2("****Hello****wide********world", "****", addEmptyStrings));
  18.     }
  19.  
  20.     private static void Display(IEnumerable<string> enumerable)
  21.     {
  22.       foreach (string item in enumerable)
  23.       {
  24.         Console.WriteLine(" - '" + item + "'");
  25.       }
  26.       Console.WriteLine();
  27.       Console.WriteLine();
  28.     }
  29.  
  30.     private static IEnumerable<string> MySplit1(string text, string delimiter, bool addEmptyStrings)
  31.     {
  32.       Console.WriteLine("Split: '" + text + "'");
  33.       string updated = text.Replace(delimiter, "\x1\x2" + delimiter + "\x1\x2");
  34.  
  35.       if (!addEmptyStrings)
  36.       {
  37.         updated = updated.Replace("\x1\x2\x1\x2", "\x1\x2");
  38.  
  39.         if (updated.StartsWith("\x1\x2", StringComparison.Ordinal))
  40.         {
  41.           updated = updated.Remove(0, 2);
  42.         }
  43.  
  44.         if (updated.EndsWith("\x1\x2", StringComparison.Ordinal))
  45.         {
  46.           updated = updated.Substring(0, updated.Length - 2);
  47.         }
  48.       }
  49.  
  50.       return (updated.Split(new[] { "\x1\x2" }, StringSplitOptions.None));
  51.     }
  52.  
  53.     private static IEnumerable<string> MySplit2(string text, string delimiter, bool addEmptyStrings)
  54.     {
  55.       Console.WriteLine("Split: '" + text + "'");
  56.       List<string> result = new List<string>(5);
  57.       int previous = 0;
  58.       while (previous < text.Length)
  59.       {
  60.         int next = text.IndexOf(delimiter, previous);
  61.         if (next < 0)
  62.         {
  63.           result.Add(text.Substring(previous));
  64.           return (result);
  65.         }
  66.  
  67.         int length = (next - previous);
  68.         if (length > 0 || addEmptyStrings)
  69.         {
  70.           result.Add(text.Substring(previous, next - previous));
  71.         }
  72.  
  73.         result.Add(delimiter);
  74.         previous = (next + delimiter.Length);
  75.       }
  76.  
  77.       if (addEmptyStrings)
  78.       {
  79.         result.Add("");
  80.       }
  81.  
  82.       return (result);
  83.     }
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement