From a69944611294feefbf2738e7d4cc1478ceee40a7 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 11 Dec 2019 00:14:15 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20String.Split(int=20chunkSize)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/README.md | 2 +- X10D/StringExtensions.cs | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/X10D/README.md b/X10D/README.md index c50d494..20ff9b0 100644 --- a/X10D/README.md +++ b/X10D/README.md @@ -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` | `X10D` | 2 | | `Random` | `X10D` | 2 | -| `string` / `SecureString` | `X10D` | 7 | +| `string` / `SecureString` | `X10D` | 8 | | `TimeSpan` | `X10D` | 1 | diff --git a/X10D/StringExtensions.cs b/X10D/StringExtensions.cs index 0cd1903..cd2a63d 100644 --- a/X10D/StringExtensions.cs +++ b/X10D/StringExtensions.cs @@ -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); } /// @@ -118,6 +119,21 @@ return builder.ToString(); } + /// + /// Splits the into chunks that are no greater than in length. + /// + /// The string to split. + /// The maximum length of each string in the returned result. + /// Returns an containing instances which are no + /// greater than in length. + public static IEnumerable 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)); + } + } + /// /// Converts a to a . ///