[HELP] Finding whole words in a string?
#1

Hey, I need some help... Is there any way on finding a whole word in a string? example: If i wanted to find the word "Brock".
Code:
My name is Brock
Would find the word Brock.

Code:
My name is Brockaleana
Would not find the word Brock.

If anyone could help me with this it would be much appreciated!
Reply
#2

Code:
if(strfind(string,"Brock",true)!=-1) {
	print("Got ya");
}
Reply
#3

Quote:
Originally Posted by Jefff
Code:
if(strfind(string,"Brock",true)!=-1) {
	print("Got ya");
}
Still doesn't do what I wanted it to do

I want it to find it like this:
My name is Brock = Found
My name is Brockaleana = Not Found
Reply
#4

Use the explode or split function by Denver, and split on space.
Reply
#5

Quote:
Originally Posted by WackoX
Use the explode or split function by Denver, and split on space.
Thank's, i'll take a look and see if i can get it working
Reply
#6

put this under OnPlayerText(playerid, text[])

Code:
new str[128];
format(str, 128, "%s", text);
			new start;
			for(new letter; letter<strlen(str); letter++)
		  {
			  start = strfind(str, "Word", true); //Checking if word from list can be found in the message
			  if(start != -1) //If The word is found
				{
					if(letter == start)
				  {
                   
				  // Do Something here
					return 1; // return 0; if you dont want the text to be shown.
	        }
				}
}
i used this code as my swear filter i hope you can use this for what you need it.
Reply
#7

pawn Code:
/**
 *  Finds a word in a string
 *  @param  string      The string to search
 *  @param  word        The word to search for
 *  @param  ignorecase  If the search is case insensitive
 *  @param  pos         The starting position for the search
 *  @return The position in the string of the begining of the word, or -1 if not found
 */

stock strfindword(const string[], const word[], bool:ignorecase=false, pos=0)
{
    if(isnull(word)) return -1;
    new wlen = strlen(word);
    if(ispacked(string)) {
        for(new i = strfind(string, word, ignorecase, pos); i != -1; i = strfind(string, word, ignorecase, ++i)) {
            if((i == 0 || string{i-1} <= ' ') && string{i+wlen} <= ' ') {
                return i;
            }
        }
    } else {
        for(new i = strfind(string, word, ignorecase, pos); i != -1; i = strfind(string, word, ignorecase, ++i)) {
            if((i == 0 || string[i-1] <= ' ') && string[i+wlen] <= ' ') {
                return i;
            }
        }
    }
    return -1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)