Get the next word in a string?
#1

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

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);
Reply
#3

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

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)
Reply
#5

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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)