Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Rextester
  10. {
  11.     public static class Ext
  12.     {      
  13.         public static string FindFirstNeedle(this string str, string[] needles, StringComparison comparison)
  14.         {
  15.             foreach (var needle in needles)
  16.             {
  17.                 var i = str.IndexOf(needle, comparison);
  18.                 if (i >= 0)
  19.                     return needle;
  20.             }
  21.             return null;
  22.         }
  23.        
  24.         public static string[] SplitIgnoreCase(this string str, string[] separators)
  25.         {
  26.             var splits = new List<string>();
  27.             var needle = str.FindFirstNeedle(separators, StringComparison.OrdinalIgnoreCase);
  28.             while (needle != null)
  29.             {
  30.                 var i = str.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
  31.                 splits.Add(str.Substring(0, i));
  32.                 str = str.Substring(i + needle.Length);
  33.                
  34.                 needle = str.FindFirstNeedle(separators, StringComparison.OrdinalIgnoreCase);
  35.             }
  36.             if (str.Length > 0)
  37.             {
  38.                 splits.Add(str);
  39.             }
  40.             return splits.ToArray();
  41.         }
  42.     }  
  43.    
  44.     public class Program
  45.     {
  46.         public static void Main(string[] args)
  47.         {
  48.             var str = "abXYcdxyefXyghKK,oo";
  49.             var splits = str.SplitIgnoreCase(new [] {"xy", "kK"});
  50.             Console.WriteLine(string.Join(",", splits));
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement