Jump to content


Photo

String Split


  • Please log in to reply
8 replies to this topic

#1 Nixa

Nixa

    GMC Member

  • GMC Member
  • 497 posts
  • Version:GM:Studio

Posted 08 February 2009 - 10:23 AM

Splits a string with given width in pixels. Splited string are saved to array. Returns number of lines (max array id):

string_split(str,width,array name) // Splits a string with given width in pixels

{
    /*
    * Splits a string with given width.
    * Argument0: string
    * Argument1: width
    * Argument2: array
    * Returns: lines (array number)
    */
    
    var split, a, b, width, lines;
 
    split = argument0;    
    split = string_replace_all(split, chr(13)+chr(10), " ");
    
    a = 1;
    b = 0;
    width = string_width(argument0);
    lines = 0;
 
    do {
        if b > 0 
        split = string_delete(split,1,string_length(variable_local_array_get(argument2,b-1)));
        do { 
            if string_width(string_copy(split,0,a)) < argument1 { 
            variable_local_array_set(argument2,b,string_copy(split,0,a));
            a += 1;
            } else break;
        }  until (split == variable_local_array_get(argument2,b ))
 
        width -= string_width(variable_local_array_get(argument2,b ));
        if width != 0
        lines += 1;
 
        a = 1;
 
        if b < lines
            b += 1;
    } until (width == 0)
    
    return lines;
 }

Edited by Nixa, 06 July 2013 - 11:56 AM.

  • 0

#2 xDanielx

xDanielx

    GMC Member

  • GMC Member
  • 1002 posts
  • Version:Unknown

Posted 09 February 2009 - 02:51 AM

It seems a bit verbose.... This is how I did it: string_split
  • 0

#3 Nixa

Nixa

    GMC Member

  • GMC Member
  • 497 posts
  • Version:GM:Studio

Posted 09 February 2009 - 06:12 PM

It seems a bit verbose.... This is how I did it: string_split

my script splits a string with given width in pixels...

create:
lines = string_split('It seems a bit verbose.... This is how I did it: string_split',32,'str_line');
draw:
for(i=0;i<lines;i+=1)
	draw_text(x,y+16*i,str_line[i]);
draws:
It se
ems
 a bi
t ver
bos
e.... 
This
 is h
ow I 
did i
t: str
ing_

Edited by Nixa, 13 March 2009 - 05:30 PM.

  • 0

#4 posva

posva

    GMC Member

  • New Member
  • 610 posts
  • Version:GM8

Posted 25 February 2009 - 04:09 PM

very good script

but it splitted 505 when i gaved it 500, it isn't a problem but only i want you to know it
  • 0

#5 BBGaming

BBGaming

    Programmer

  • GMC Member
  • 2461 posts
  • Version:GM7

Posted 25 February 2009 - 07:06 PM

So it's basically a string wrap script, except it saves the results to an array?
  • 0

#6 Nixa

Nixa

    GMC Member

  • GMC Member
  • 497 posts
  • Version:GM:Studio

Posted 13 March 2009 - 04:18 PM

very good script

but it splitted 505 when i gaved it 500, it isn't a problem but only i want you to know it

Fixed...

So it's basically a string wrap script, except it saves the results to an array?

Yup... It's great for chat with multilines.
  • 0

#7 posva

posva

    GMC Member

  • New Member
  • 610 posts
  • Version:GM8

Posted 17 March 2009 - 10:53 PM

thanks will download again next time, have no tiem right now
  • 0

#8 ze1

ze1

    GMC Member

  • GMC Member
  • 147 posts
  • Version:GM8

Posted 29 June 2013 - 08:14 PM

I'm having problems using this script when I have a string split into lines. =(

 

For example:

text = " This is a
test."

lines = string_split(text,32,'str_line');

It returns an error saying that the array limit size was reached.

 

It would also be nice if the script split strings only at space or '-' characters... splitting words in half is kinda weird.


Edited by ze1, 29 June 2013 - 08:48 PM.

  • 0

#9 Nixa

Nixa

    GMC Member

  • GMC Member
  • 497 posts
  • Version:GM:Studio

Posted 06 July 2013 - 11:55 AM

It returns an error saying that the array limit size was reached.

New line somehow messes up the script..

Just add this line:

split = string_replace_all(split, chr(13)+chr(10), " ");

Edit:

I've updated the script..


Edited by Nixa, 06 July 2013 - 11:57 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users