Added (array of array).Flatten extension.
[IEnumerableExtras.git] / C# / StringExtension.cs
blob604e85d24fad0153850e19034b98dc53078bc75a
1 using System.Collections.Generic;
3 namespace IEnumerableExtras
5 /// <summary>
6 /// Provides extension method to split a <see cref="string"/> while keeping the separator.
7 /// </summary>
8 public static class StringExtension
10 /// <summary>
11 /// Split this string keeping separator in the result.
12 /// </summary>
13 /// <param name="str"></param>
14 /// <param name="value"></param>
15 /// <returns></returns>
16 public static IEnumerable<string> SplitAround( this string str, string value )
18 var res = new List<string>();
19 var l = value.Length;
20 var n = str.Length;
21 var i = 0;
25 var j = str.IndexOf( value, i == 0 ? 0 : i + 1 );
27 if ( j != -1 )
29 if ( j != i )
31 res.Add( str.Substring( i, j - i ) );
34 res.Add( str.Substring( j, l ) );
35 i = j + l;
37 else
39 res.Add( str.Substring( i ) );
40 break;
43 while ( i < n );
45 return res;