Extracting parts of a string.
#1

I feel really stupid because this seems like such a simple thing to do, yet I can't find a way to do it!

Anyways, for example I have the name "Russell_Rhodes" and I want to extract "Russell" and store that into a variable (i.e. gFirstName[playerid]) and then do the same with "Rhodes" (i.e. gLastName[playerid]). Of course, I am obtaining a player's name and will need to work from just figuring out what's before (and after) the underscore. How could I do that?
Reply
#2

https://sampwiki.blast.hk/wiki/Strmid
And then Strfind to find the place you want to extract to / from.
https://sampwiki.blast.hk/wiki/Strfind
Reply
#3

maybe sscanf2 can do that for you
Код:
sscanf(Name,"p<_>s[24]s[24]",FirstName,LastName);
Reply
#4

I'm sorry for asking a question in this thread, but what does that <_> mean in the middle of the sscanf? (I'm feeling stupid now)
Reply
#5

I would use split from SA-MP Wiki
pawn Код:
// Author unknown. It was probably someone smart like [[User:DracoBlue|DracoBlue]] or [[User:******|******]].
stock split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc))
    {
        if(strsrc[i] == delimiter || i == strlen(strsrc))
        {
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
    return 1;
}

new name[2][12];
split(playername,name,'_');
//name[0] = Before _
//name[1] = After _
Reply
#6

OT: strmid should do the job

Quote:
Originally Posted by Max_Coldheart
Посмотреть сообщение
I'm sorry for asking a question in this thread, but what does that <_> mean in the middle of the sscanf? (I'm feeling stupid now)
Well basically, it's part of the delimiter specifier, which tells sscanf when to split a string:
pawn Код:
sscanf("Brown:Poop", "p<:>s[24]s[24]", st1, st2);
This would split the string in two pieces: 'Brown' and 'Poop'.

Also: https://sampforum.blast.hk/showthread.php?tid=120356
Reply
#7

Thanks guys! I'm thinking of using sscanf as it seems to be the easiest thing, but there were two other good options: strmid and split

The question is, which one is more efficient?
Reply
#8

Sscanf is a plugin and is very fast especially with large strings. Split is just a function that utilizes strmid, which you can also do yourself to make it more effecient for your needs. I'd go for sscanf just because it's the most simple and if there'd be a difference in speed it's minimal.
Reply
#9

Quote:
Originally Posted by playbox12
Посмотреть сообщение
Sscanf is a plugin and is very fast especially with large strings. Split is just a function that utilizes strmid, which you can also do yourself to make it more effecient for your needs. I'd go for sscanf just because it's the most simple and if there'd be a difference in speed it's minimal.
Okay, thanks bud!
Reply
#10

If you'd only need one piece then strmid would be fine, but in your case I'd go with sscanf. I would definitely not recommend using split.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)