Using strfind
#1

Is there a way that I can detect if there is not a specific character in a string?

I've only found tutorials that explains how to find a character in a string, such as:

PHP код:
if(strfind(inputtext" ") != -1// Finds a blank space 
But what I need is to find if the player did not insert the character. For example a dot "." How can I check if there are no dots in the string?
Reply
#2

pawn Код:
"."
?
pawn Код:
if(strfind(inputtext, ".", true) != -1)
Reply
#3

That's exactly what I don't want.

I want to detect if there ARE NOT any dots on the string, the code you just posted detects if there ARE dots in the string.

Using (!strfind) doesn't seem to work.
Reply
#4

Try:

Код:
if(strfind(inputtext, ".", true) == -1)
Not sure if it's best way (as it finds for string),

But you could also use this to find a single character:
Код:
HasDot(string[])
{
	new len = strlen(string);
	for(new i = 0; i < len; i++)
	{
		if(string[i] == '.') return true;
	}
	return false;
}
then you would use this:
Код:
if(!HasDot(inputtext))
Reply
#5

You can even do this with "tutorials that explain how to find characters in a string":
pawn Код:
// ** INCLUDES

#include <a_samp>

// ** MAIN

main()
{
    print("Loaded \"find_string_in_string.amx\".");

    if(ContainsStringInString("Hello. How are you?", "."))
    {
        print("Specified string was found.");
    }
    else
    {
        print("Specified string wasn't found.");
    }
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

forward bool:ContainsStringInString(const search[], const find[]);
public bool:ContainsStringInString(const search[], const find[])
{
    if(strfind(search, find) != -1) return true;
    return false;
}
And by using some common sense while studying the condition "if(strfind(search, find) != -1)", you'd notice the following: If what is returned by the function is not -1, means that the specified string was found. Then what is -1, would mean that the specified string was found.

pawn Код:
if(strfind(search, find) == -1) return false;
@RoboN1X - Once you're done with the loop, use break then return the value afterwards. And there is no need to use loops whatsoever in this case, use strfind when it comes to finding strings in non-defined spots in other strings. If you want to compare one cell, then you could use "if(text[0] == '.')" for example.
Reply
#6

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
@RoboN1X - Once you're done with the loop, use break then return the value afterwards. And there is no need to use loops whatsoever, just use strfind when it comes to finding strings in non-defined spots in other strings. If you want to compare one cell, then you could use "if(text[0] == '.')" for example.
I'm sorry, isn't return keyword automatically breaks loop and exit the function? Also i'm just telling "it could be also", since he is just finding a CHARACTER in a STRING, strfind is for finding SUBSTRING in a STRING. Also he didn't say to compare one cell, he said that if there is/are ANY dot(s) in a STRING.
Althought i didn't compare which is best method for this case, i'm just giving two possible methods.

Please correct me if i'm wrong, thanks.

And for thread starter, hope that helps.
Reply
#7

Thanks for the explanation, I thought there was a shorter way to do this.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)