strfind question
#1

Код:
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?
Reply
#2

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

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
Reply
#4

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

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".
Reply
#6

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

...
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??
Reply
#8

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

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

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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)