using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OTPNet { public static class StringExtensions { /// Returns a string array that contains the substrings in this string that are seperated a given fixed length. /// This string object. /// Size of each substring. /// CASE: length > 0 , RESULT: String is split from left to right. /// CASE: length == 0 , RESULT: String is returned as the only entry in the array. /// CASE: length < 0 , RESULT: String is split from right to left. /// /// String array that has been split into substrings of equal length. /// /// /// string s = "1234567890"; /// string[] a = s.Split(4); // a == { "1234", "5678", "90" } /// /// public static string[] Split(this string s, int length) { System.Globalization.StringInfo str = new System.Globalization.StringInfo(s); int lengthAbs = Math.Abs(length); if (str == null || str.LengthInTextElements == 0 || lengthAbs == 0 || str.LengthInTextElements <= lengthAbs) return new string[] { str.ToString() }; string[] array = new string[(str.LengthInTextElements % lengthAbs == 0 ? str.LengthInTextElements / lengthAbs : (str.LengthInTextElements / lengthAbs) + 1)]; if (length > 0) for (int iStr = 0, iArray = 0; iStr < str.LengthInTextElements && iArray < array.Length; iStr += lengthAbs, iArray++) array[iArray] = str.SubstringByTextElements(iStr, (str.LengthInTextElements - iStr < lengthAbs ? str.LengthInTextElements - iStr : lengthAbs)); else // if (length < 0) for (int iStr = str.LengthInTextElements - 1, iArray = array.Length - 1; iStr >= 0 && iArray >= 0; iStr -= lengthAbs, iArray--) array[iArray] = str.SubstringByTextElements((iStr - lengthAbs < 0 ? 0 : iStr - lengthAbs + 1), (iStr - lengthAbs < 0 ? iStr + 1 : lengthAbs)); return array; } } }