How to identify a String value among a range of values

Hey,

I'm testing some things with this library and I'm stuck on this part which seems to be quite simple.

The question is: If I do this equality I can get the result on the serial monitor:

https://github.com/GyverLibs/FastBot


if(msg.text == "9") {
  Serial.println("OK");
}

But if I enter any value in Telegram BOT that is between the two values ​​below, nothing happens. There is no result on the serial monitor.

if(msg.text > "9" && msg.text < "251") {
  Serial.println("OK");
}

Does anyone know why ?
Thanks

You are comparing Strings which do not have a particular order like numbers.
If the strings really always only contain digits 0,1,2,3,4,5,6,7,8,9
you can convert them into integers. And with the integers you can do
compare ">" greater than or "<" smaller than

I had tried to do this, but I think I tried it wrong. Now I did it like this, and it seems to be working. Then I came here and saw your message.

I thank.

int intmsgtexto = msg.text.toInt();
    if(intmsgtexto > 9 && intmsgtexto < 251) {

Yes they do. The String class overloads comparison operators. Amongst others.

void setup() {
  Serial.begin(115200);

  String x = "abc";
  String y = "def";
  String z = "ghi";

  Serial.print(y);
  if (y > z)
    Serial.print(" is greater than ");
  else 
    Serial.print(" is less    than ");
  Serial.println(z);

  Serial.print(y);
  if (y > x)
    Serial.print(" is greater than ");
  else 
    Serial.print(" is less    than ");
  Serial.println(x);
}

void loop() {
  // put your main code here, to run repeatedly:
}

def is less than ghi
def is greater than abc

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.