SA-MP Forums Archive
Get the next word in a string? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Get the next word in a string? (/showthread.php?tid=277571)



Get the next word in a string? - linuxthefish - 18.08.2011

Hi, how do i get the next word in a string? For example;

Quote:

Name: John - Color: Orange - Age: 29 - IP: 127.0.0.1

How would i extract the word after "color:"? I think i should use sscanf, but i'm not sure how...


Re: Get the next word in a string? - Jefff - 18.08.2011

pawn Код:
new dest[24];
new str[] = "Name: John - Color: Orange - Age: 29 - IP: 127.0.0.1";
new abc = strfind(str,"Color:");
strmid(dest,str,abc+7,strfind(str,"-",true,abc+7)-1);
print(dest);



Re: Get the next word in a string? - RyDeR` - 18.08.2011

You should check if strfind doesn't return '-1' to be sure.


Re: Get the next word in a string? - iPLEOMAX - 18.08.2011

pawn Код:
printf( "%s", strnext("Name: John - Color: Orange - Age: 29 - IP: 127.0.0.1", "Color:", "-") );

stock strnext( const source[], sub[], endchar[] )
{
    new result[32], strt, endstr, len = strlen(source);
    strt = strfind(source, sub, false)+strlen(sub)+1;
    endstr = strfind(source, endchar, true, strt+strlen(sub));
    if(!strlen(endchar)) { strmid(result,source,strt,len); }
    else { strmid(result,source,strt,endstr); }
    return result;
}

//Other Example:
new string[] = "Name: Evil Man || Suspect: Drama Queen || Location: Notting Hills || Combat: Ninja";
   
printf( "%s", strnext(string, "Name:", "||"));
printf( "%s", strnext(string, "Suspect:", "||"));
printf( "%s", strnext(string, "Location:", "||"));
printf( "%s", strnext(string, "Combat:", ""));

//prints:
Evil Man
Drama Queen
Notting Hills
Ninja
Edited.
Idk how should i do the -1 check for strfind.. x)


Re: Get the next word in a string? - linuxthefish - 18.08.2011

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
new dest[24];
new str[] = "Name: John - Color: Orange - Age: 29 - IP: 127.0.0.1";
new abc = strfind(str,"Color:");
strmid(dest,str,abc+7,strfind(str,"-",true,abc+7)-1);
print(dest);
Thanks, that works

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
You should check if strfind doesn't return '-1' to be sure.
I don't think there is going to be a error, as i already have built in error checking in the bit which produces the string.

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
pawn Код:
printf( "%s", strnext("Name: John - Color: Orange - Age: 29 - IP: 127.0.0.1", "Color:", "-") );

stock strnext( const source[], sub[], endchar[] )
{
    new result[32], strt, endstr, len = strlen(source);
    strt = strfind(source, sub, false)+strlen(sub)+1;
    endstr = strfind(source, endchar, true, strt+strlen(sub));
    if(!strlen(endchar)) { strmid(result,source,strt,len); }
    else { strmid(result,source,strt,endstr); }
    return result;
}

//Other Example:
new string[] = "Name: Evil Man || Suspect: Drama Queen || Location: Notting Hills || Combat: Ninja";
   
printf( "%s", strnext(string, "Name:", "||"));
printf( "%s", strnext(string, "Suspect:", "||"));
printf( "%s", strnext(string, "Location:", "||"));
printf( "%s", strnext(string, "Combat:", ""));

//prints:
Evil Man
Drama Queen
Notting Hills
Ninja
Edited.
Idk how should i do the -1 check for strfind.. x)
Thanks, this will be useful in a later project