Split a string
#1

Hey, I got 3 numbers which a person gave me via a dialog. So it's saved in inputtext. These 3 numbers are already split with a ":". For example: 1:324:26126

Now I want to have each part in a extra string without the ":". For example: string1: 1, string2: 324, string3: 26126

How to do that?
Reply
#2

sscanf2/unformat
Reply
#3

pawn Код:
new number1;
new number2;
new number3;
sscanf(inputtext, "p<:>iii", number1, number2, number3);
Hope it works
Reply
#4

How can I remove the ":" with sscanf?
Reply
#5

It will remove it automatically
Reply
#6

pawn Код:
new number1, number2, number3, str[256];
sscanf(inputtext, "p<:>iii", number1, number2, number3);
format(str, sizeof(str), "1. %i 2. %i 3. %i", number1, number2, number3);
SendClientMessage(playerid, COLOR_RED, str);
This is now showing the results. Well, when I insert 01:02:03 it says: number1 = 1 number2 = 2 number3 = 3 (1. 1 2. 2 3. 3)
Reply
#7

pawn Код:
new number1, number2, number3, str[256];
sscanf(inputtext, "p<:>iii", number1, number2, number3);
format(str, sizeof(str), "1. 0%i 2. 0%i 3. 0%i", number1, number2, number3);
SendClientMessage(playerid, COLOR_RED, str);
I can't think of any other way atm
Reply
#8

That's not good because I really need it. Anyone got an idea?

EDIT: And how can I check if there are 3 ":" in the string?
Reply
#9

Quote:
Originally Posted by GiS
Посмотреть сообщение
That's not good because I really need it. Anyone got an idea?

EDIT: And how can I check if there are 3 ":" in the string?
Loop.

Use it as so: stringCount("1:2:3", ":", 5);
pawn Код:
stock stringCount(string[], token, length)
{
    new count;
    for(new i = 0; i < length; i++)
    {
        if(string[i] == token)
            count++;
    }
    return count;
}
Reply
#10

Quote:
Originally Posted by GiS
Посмотреть сообщение
That's not good because I really need it. Anyone got an idea?

EDIT: And how can I check if there are 3 ":" in the string?
https://sampwiki.blast.hk/wiki/Strfind as i know
Reply


Forum Jump:


Users browsing this thread: