Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recently had to write something that accomplishes this at work, so I thought I would post my solution to this problem. As an added bonus, the functionality of this solution provides a way to split the string in the opposite direction and it does correctly handle unicode characters as previously mentioned by Marvin Pinto above. So, here it is:</p> <pre><code>using System; using Extensions; namespace TestCSharp { class Program { static void Main(string[] args) { string asciiStr = "This is a string."; string unicodeStr = "これは文字列です。"; string[] array1 = asciiStr.Split(4); string[] array2 = asciiStr.Split(-4); string[] array3 = asciiStr.Split(7); string[] array4 = asciiStr.Split(-7); string[] array5 = unicodeStr.Split(5); string[] array6 = unicodeStr.Split(-5); } } } namespace Extensions { public static class StringExtensions { /// &lt;summary&gt;Returns a string array that contains the substrings in this string that are seperated a given fixed length.&lt;/summary&gt; /// &lt;param name="s"&gt;This string object.&lt;/param&gt; /// &lt;param name="length"&gt;Size of each substring. /// &lt;para&gt;CASE: length &amp;gt; 0 , RESULT: String is split from left to right.&lt;/para&gt; /// &lt;para&gt;CASE: length == 0 , RESULT: String is returned as the only entry in the array.&lt;/para&gt; /// &lt;para&gt;CASE: length &amp;lt; 0 , RESULT: String is split from right to left.&lt;/para&gt; /// &lt;/param&gt; /// &lt;returns&gt;String array that has been split into substrings of equal length.&lt;/returns&gt; /// &lt;example&gt; /// &lt;code&gt; /// string s = "1234567890"; /// string[] a = s.Split(4); // a == { "1234", "5678", "90" } /// &lt;/code&gt; /// &lt;/example&gt; 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 &lt;= lengthAbs) return new string[] { str.ToString() }; string[] array = new string[(str.LengthInTextElements % lengthAbs == 0 ? str.LengthInTextElements / lengthAbs: (str.LengthInTextElements / lengthAbs) + 1)]; if (length &gt; 0) for (int iStr = 0, iArray = 0; iStr &lt; str.LengthInTextElements &amp;&amp; iArray &lt; array.Length; iStr += lengthAbs, iArray++) array[iArray] = str.SubstringByTextElements(iStr, (str.LengthInTextElements - iStr &lt; lengthAbs ? str.LengthInTextElements - iStr : lengthAbs)); else // if (length &lt; 0) for (int iStr = str.LengthInTextElements - 1, iArray = array.Length - 1; iStr &gt;= 0 &amp;&amp; iArray &gt;= 0; iStr -= lengthAbs, iArray--) array[iArray] = str.SubstringByTextElements((iStr - lengthAbs &lt; 0 ? 0 : iStr - lengthAbs + 1), (iStr - lengthAbs &lt; 0 ? iStr + 1 : lengthAbs)); return array; } } } </code></pre> <p>Also, here is an image link to the results of running this code: <a href="http://i.imgur.com/16Iih.png">http://i.imgur.com/16Iih.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload