Split string every 20 chars and add newline

Traversal

Member
Having a little problem on how to split my string into chunks, so I can display it properly.

global.inputdata = "some long text inside my string here....";

I would like to display it with new lines every let's say 20 chars, but do not want to crack it
in the middle of a word.

The output should be something like this:

"some long text" //14 chars
"inside my string" //16 chars
"here...." //the rest goes here 8 chars
EDIT: need to have those splittet strings inside variables, so draw_text_ext() would only help with drawing
 
Last edited:

Traversal

Member
Thank you. Beside drawing it on the screen, I need some string handling for further splitting and such things.
So draw_text_ext() would only come in handy for drawing.
 
Thank you. Beside drawing it on the screen, I need some string handling for further splitting and such things.
So draw_text_ext() would only come in handy for drawing.
You can just loop through it and add the \n character every 20 incrementation. This is highly innefficient, and this piece of code doesn<t take spaces into account, so you'll have to tweak it a lot to make it pretty, but you get the idea.
GML:
str = "hello, this is a very very long string that I could easily have replaced with Ipsum Lorem, or whatever poo that thing is called";
for(var i=0; i<string_length(str); i=i+10){
    str = string_insert("\n", str, i);  
}
 

Nidoking

Member
for(var i=0; i<string_length(str); i=i+10){ str = string_insert("\n", str, i); }
Not only does this fail to take spaces into account, it also doesn't place the line breaks correctly. You're only going to place a line break every nine characters after the first break, because you're not accounting for the string length increasing as you add line breaks to it.

And can you pass 0 to string_insert? I think the first iteration is going to throw a runtime error.
 
Not only does this fail to take spaces into account, it also doesn't place the line breaks correctly. You're only going to place a line break every nine characters after the first break, because you're not accounting for the string length increasing as you add line breaks to it.

And can you pass 0 to string_insert? I think the first iteration is going to throw a runtime error.
No runtime error, why could't you start a string with a new line?
I've already mentionned all that right in the post, but hey, how nice of you to chime-in to repeat everything I've said, I feel like I have an apostle preachig my every word!
 
Top