Extracting parts of a string. -
Scenario - 04.12.2011
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?
Re: Extracting parts of a string. -
[HiC]TheKiller - 04.12.2011
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
Re: Extracting parts of a string. -
Babul - 04.12.2011
maybe sscanf2 can do that for you
Код:
sscanf(Name,"p<_>s[24]s[24]",FirstName,LastName);
Re: Extracting parts of a string. - Max_Coldheart - 04.12.2011
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)
Re: Extracting parts of a string. -
[MG]Dimi - 04.12.2011
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 _
Re: Extracting parts of a string. -
Hiddos - 04.12.2011
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
Re: Extracting parts of a string. -
Scenario - 04.12.2011
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?
Re: Extracting parts of a string. -
playbox12 - 04.12.2011
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.
Re: Extracting parts of a string. -
Scenario - 04.12.2011
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!
Re: Extracting parts of a string. -
Vince - 04.12.2011
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.