SA-MP Forums Archive
strfind question - 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)
+--- Thread: strfind question (/showthread.php?tid=599825)



strfind question - Aa12 - 30.01.2016

Код:
format(string,sizeof(string),"%d\t%s\n",GetPVarInt(playerid,use[i]), use[i]);
I need to get %s (without %d) using strfind ( https://sampwiki.blast.hk/wiki/Strfind ) function, how do I do that?


Re: strfind question - Tamy - 30.01.2016

Код:
if(strfind(string, use[i], true) != -1) print("Successfully found in the string.");
else print("Could not find.");



Re: strfind question - Aa12 - 30.01.2016

Quote:
Originally Posted by Tamy
Посмотреть сообщение
Код:
if(strfind(string, use[i], true) != -1) print("Successfully found in the string.");
else print("Could not find.");
No, I need to find string using only "string" variable, imagine if "use[i]" isnt even there


Re: strfind question - Sascha - 30.01.2016

Quote:
Originally Posted by Aa12
Посмотреть сообщение
No, I need to find string using only "string" variable, imagine if "use[i]" isnt even there
Then you'd better specify what you really want to search for, 'cause if you want to search for what '%s' shadows, you'll have to do what was said before by Tamy


Re: strfind question - Aa12 - 31.01.2016

format(string,sizeof(string),"%d\t%s\n",GetPVarInt (playerid,use[i]), use[i]);

so for example

GetPVarInt(playerid,use[i]) is 10
use[i] is "good"

"string" would be :
Код:
10\tgood\n
(I use this for player dialog (style list))

Now I need to get word "good" from this string with strfind function, using only variable "string".


Re: strfind question - Virtual1ty - 31.01.2016

Still, what for would you need this since you already have the string in "use" and you also have the playerid associated with it.
Anyways this works. You have to use strdel to delete portions from the string. Strfind only returns the position of where that substring occurs in the main string.
Код:
new found = strfind(string, use[i]);
if (found != -1) strdel(string, 0, found), strdel(string, strlen(use[i]), strlen(use[i])+2);
"string" now contains the word "good". Keep in mind this example is hardcoded for your situation. The first "strdel" deletes everything from the beggining of the "string" to the beggining of the word "good" and the second one deletes the 2 excess characters at the end which are newline "\n".


Actually, you could've also used strmid as that's what it's ACTUALLY for, and not for copying strings!! Code now becomes:
Код:
new found = strfind(string, use[i]);
if (found != -1) strmid(string, string, found, found+strlen(use[i]));
Edit: to break this last part of code to you:
We introduce a new variable "found". It contains the position in "string" where that "use[i]" (e.g. "good") string occurs. If it is -1 then it is not found in "string". "strmid" extracts portions of a string and saves it to another string. The parameters are documented on the Wiki. We want to extract everything in the "string" from "found" to "found" + length of "use[i]" (e.g. "good" is 4 chars). All this gets stored back to variable "string".

Hope it makes it clearer for you. Have a good day.


Re: strfind question - Aa12 - 31.01.2016

...
Ok new example
I have string which has stored text in it "10\tgood\n" . Text "good" can be any other word, for example "bad", "wrong" and so on. Same with "10", it can be any other number, only \t and \n stays the same.
Now, how do I get text between \t and \n??


Re: strfind question - Virtual1ty - 31.01.2016

Use "strmid". See my example. However, you don't have to do this since you already have the word (e.g. "good") stored in "use[i]". Please explain the exact use of this.


Re: strfind question - PrO.GameR - 31.01.2016

Well, thats not a job for strfind alone, you should use strmid too
PHP код:
strmid(dest[], const source[], startendmaxlength=sizeof dest)
strmid(string2,string,strfind(string,"\t")+1,strfind(string,"\n")); 
now string2 contains whatever there is in string between \t and \n (aka your %s in this case)


Re: strfind question - Aa12 - 31.01.2016

Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
Well, thats not a job for strfind alone, you should use strmid too
PHP код:
strmid(dest[], const source[], startendmaxlength=sizeof dest)
strmid(string2,string,strfind(string,"\t")+1,strfind(string,"\n")); 
now string2 contains whatever there is in string between \t and \n (aka your %s in this case)
Thank you!