How to split long Text into multiple chunks?

 

Hey there.

Axy has a cool subset of tools to modify strings.
But I can't seem to find a tool that allows me to brake up long text into chunks based on the character count.

In pseudocode, what I want to do is:

string = "Hello my dear fellows. This string contains quite a few characters"
string1 = string:sub(1,50)
string2 = string:sub(51)

string1 - "Hello my dear fellows. This string contains quite "
string2 - "a few characters"

Funny enough it works exactly like this with a script compound...:-)compound...:-)

--$ inString string --$ inLength integer --$ outString1 string --$ outString2 string outString1 = inString:sub(1, inLength) outString2 = inString:sub(inLength + 1)




   EricWest

 
Profile Image
Eifert@Aximmetry
  -  

Hi Eric,

You could use the Text Slice module.
Just set Count to 50 and Start Index to 50 for the second slice, like here:

Note this module can be a bit confusing if you have a programming background.
It doesn't just function as string.substring(), but also as string.split().
If you don't change Item Size. Then Start Index is the character's position where the string will start and the Count is the length of the string.

Item Size is useful when you are slicing the Text with the Separators. By increasing its value, it will also contain the Separators...

Warmest regards,



 
Profile Image
EricWest
  -  

Hi Gabor.

Cool! Thanks for the tip!
This  makes me wonder though if there's a noticeable difference in speed between a simple script compound as above
and using the built-in TextSlice.
Most likely somewhere in the nanoseconds realm, but given the speed of LuaJIT I would guess that for such a task the script might be a tiny little bit faster....?

Cheers & all the best.

Eric.


 
Profile Image
Eifert@Aximmetry
  -  

Hi Eric,

I am not sure which one is faster, but yes it will be in the nanoseconds.
At 30 fps, my laptop could execute 50 000 000 times your script at every frame:

for i=1,50000000 do
outString1 = inString:sub(1, inLength)
outString2 = inString:sub(inLength + 1)
end

So, I would suggest to not bother optimizing things like that.
In my personal experience, It is better if your project is clean, transparent, and easily readable than doing small optimizations like that.

Warmest regards,