Add String.Split(int chunkSize)

This commit is contained in:
Oliver Booth 2019-12-11 00:14:15 +00:00
parent 9b5e02b5a1
commit a699446112
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
2 changed files with 19 additions and 3 deletions

View File

@ -23,5 +23,5 @@ Below is a list of the number of extension methods written for a given type. Ove
| `long` / `ulong` | `X10D` | 11 |
| `IList<T>` | `X10D` | 2 |
| `Random` | `X10D` | 2 |
| `string` / `SecureString` | `X10D` | 7 |
| `string` / `SecureString` | `X10D` | 8 |
| `TimeSpan` | `X10D` | 1 |

View File

@ -3,6 +3,7 @@
#region Using Directives
using System;
using System.Collections.Generic;
using System.Net;
using System.Security;
using System.Text;
@ -64,7 +65,7 @@
if (value.Length == 0)
{
throw new ArgumentException("Must specify valid information for parsing in the string.",
nameof(value));
nameof(value));
}
Type t = typeof(T);
@ -74,7 +75,7 @@
throw new ArgumentException("Type provided must be an Enum.", "T");
}
return (T)Enum.Parse(t, value, ignoreCase);
return (T) Enum.Parse(t, value, ignoreCase);
}
/// <summary>
@ -118,6 +119,21 @@
return builder.ToString();
}
/// <summary>
/// Splits the <see cref="String"/> into chunks that are no greater than <paramref name="chunkSize"/> in length.
/// </summary>
/// <param name="str">The string to split.</param>
/// <param name="chunkSize">The maximum length of each string in the returned result.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing <see cref="String"/> instances which are no
/// greater than <paramref name="chunkSize"/> in length.</returns>
public static IEnumerable<string> Split(this string str, int chunkSize)
{
for (int i = 0; i < str.Length; i += chunkSize)
{
yield return str.Substring(i, Math.Min(chunkSize, str.Length - i));
}
}
/// <summary>
/// Converts a <see cref="String"/> to a <see cref="SecureString"/>.
/// </summary>