Split a string every 'n' characters?

I know how to use String.Split(""[0]); but I need to split a string every 3 characters rather than by a character such as “_”. I need the split string to be returned as an array and using Substring would be way too slow (I’m handling lots of data).

How would I do this?

I doubt that Substring would be any slower, but that's also a way to split the string.

// (I'll assume you use UnityScript? I prefer C# but that's up to you)
function Split(text : String, charCount : int) : String[]
{
    if (text.length == 0)
        return new String[0];
    var arrayLength = (text.length-1) / charCount +1;
    var result = new String[arrayLength];
    for(var i = 0; i < arrayLength; i++)
    {
        var tmp = "";
        for (var n = 0; n < charCount; n++)
        {
            var index = i*charCount+n;
            if (index >= text.length)   //important if last "part" is smaller
                break;
            tmp += text[index];
        }
        result *= tmp;*
 *}*
 *return result;*
*}*
*```*
*<p>I'm not sure why you think that Substring is slow? of course you need a lot function calls which might be the biggest problem. I guess this is not that much slower since Substring does something similar (probably even more efficient :D)</p>*
*```*
*function Split(text : String, charCount : int) : String[]*
*{*
 *if (text.length == 0)*
 *return new String[0];*
 *var arrayLength = (text.length-1) / charCount +1;*
 *var result = new String[arrayLength];*
 *for(var i = 0; i < arrayLength-1; i++)*
 *{*
 <em>result _= text.Substring(i*3,3);_</em>
 _*}*_
 <em>_result[arrayLength-1] = text.Substring((arrayLength-1)*3);_</em> 
 _*return result;*_
_*}*_
_*```*_
_*<p><strong><em>edit</em></strong>*_
_*I've forgot to handle the special case when calling the method with an empty string. In that case it would also return one element that is empty due to my arrayLength calculation. I've added a check at the start and return an empty array in that case.</p>*_

How about handling this at the time you load it? If you can get a stream you can do something like:

    BinaryReader tReader = new BinaryReader(tMyStream);

    string[] tMyStrings = new string[tMyStream.Length / 3];
    for (int i = 0; i < tMyStrings.Length; i++)
        tMyStrings *= new string(tReader.ReadChars(3));*

This assumes a few things, but you can probably make it work.

If the data you are trying to handle is not entered by a user you could again use Split , and split the data each time a special character is seen. This could be applied of the user also enters the data , just instruct them to split the data by spaces and then you can replace the Space with said character. You could use something like "&" , or if the data you are mentioning uses such characters , you can split by "&split". I apologise for the simplistic solution but i am unaware of anything else.

There might be something but im not sure if it will work. You can try

String.Split(""[3]);

This will read out your file piece by piece and you don't have to load all the 95mb into memory.

` string path = "path to file";

System.IO.StreamReader reader = new System.IO.StreamReader(path);

int index = 0;

while (!reader.EndOfStream)

{

char buf = new char[3];

reader.ReadBlock(buf, index, 3);

index += 3;

}
`

Well, that’s how I solved it for my purpose.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class StringExtensions
{
    public static List<string> SplitEveryN(this string str, int n = 1024)
    {
        List<string> ret = new List<string>();

        int chunkIterator = 0;
        while (chunkIterator < str.Length)
        {
            int currentChunkSize = Mathf.Min(n, str.Length - chunkIterator);
            ret.Add(str.Substring(chunkIterator, currentChunkSize));
            // Debug.Log(str.Substring(chunkIterator, currentChunkSize));
            chunkIterator += currentChunkSize;
        }
        return ret;
    }
}

To use it, it’s just to call

// default length of 1024
foreach (var stringChunk in myLongString.SplitEveryN())
{
    Debug.Log(stringChunk);
}
// custom length
foreach (var stringChunk in myLongString.SplitEveryN(32))
{
    Debug.Log(stringChunk);
}