SA-MP Forums Archive
Split a string into two strings - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Split a string into two strings (/showthread.php?tid=502381)



Split a string into two strings - Gilmar - 23.03.2014

Hi, I was writing my own /me cmd for a RolePlay gamemode and I wanted to split the /me string into a two strings when it's too long.
I wrote this function:
pawn Код:
public SplitMe(textme[], len, first[], second[])
{
    new i = 0;
    new j = 0;
    while(i<40)
    {
        first[i] = textme[i];
        i++;
    }
    first[i+1] = '\0';
    while(i<len)
    {
        second[j] = textme[i];
        i++;
        j++;
    }
    return 1;
}
In this example the strlen of the first string is 40.
When I try to show the two strings with a SendClientMessage it does show just the second string!
I thought could be a problem with the string terminator so I changed this line:
pawn Код:
first[i+1] = '\0';
With:
pawn Код:
first[i+1] = 0;
With this fix the problem is just the same.
Someone can help me please? What's the problem?


Re: Split a string into two strings - Vince - 23.03.2014

Why reinvent the wheel? https://sampwiki.blast.hk/wiki/Strmid


Re: Split a string into two strings - Niko_boy - 23.03.2014

you can just do
pawn Код:
strmid(secondstring, mainstring, 40, strlen(string)); //get part of string from 40th character to end of mainstring.
and then
pawn Код:
strdel(mainstring, 40, strlen(string)); // deletes string part from 40th character to end of string
Hence you can now use mainstring as your first message and second and your second.
Elaborate this method more to get exactly what you need.

I recommend you something like i said because this would be faster than rather looping into strings and then splitting them out.


Re: Split a string into two strings - Gilmar - 23.03.2014

Thanks! I fix my problem. I didn't know this string function, sorry.