Split a string into two strings
#1

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?
Reply
#2

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

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.
Reply
#4

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


Forum Jump:


Users browsing this thread: 2 Guest(s)