Detecting if a string contains a number

I am trying to detect if my string has a number and I can't find a solution anywhere online! My string is X100Y60

Is it a string or a String ?

There is a difference

Please post an example of the string/String in a sketch. What is the source of the string/String ? Do you have any control over its format and contents ?

I am trying to detect if my string has a number and I can’t find a solution anywhere online! My string is X100Y60

Do you literally just want to know if any of X100Y60 is a digit?

// is there a number in X100Y60?

char myArray[] = "X100Y60"; //should give a yes
//char myArray[] = "abcdefg"; //should give a no
byte lengthOfMyArray = sizeof(myArray) / sizeof(myArray[0]);
bool containsANumber = false;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < lengthOfMyArray - 1; i++)
  {
    if (isDigit(myArray[i])) containsANumber = true;
  }
  if (containsANumber) Serial.println("Yes"); else  Serial.println("No");
  } //setup

void loop()
{

} //loop

You may as well break as soon as you find a digit, then not only do you know you’ve got one, you also know where it is

Indeed, I might as well. Also might as well not.

The example string or String provided by the OP, X100Y60, looks suspiciously like X/Y coordinates, so my bet is that he/she wants the 100 and 60 extracted to variables, but we will have to wait for details

UKHeliBob:
The example string or String provided by the OP, X100Y60, looks suspiciously like X/Y coordinates, so my bet is that he/she wants the 100 and 60 extracted to variables, but we will have to wait for details

It is the communication between a C# form application and an arduino to move servos

And so what is your question?

(Since I think you already know the answer to

I am trying to detect if my string has a number

... is that it does?)

jubukraa:
Do you literally just want to know if any of X100Y60 is a digit?

// is there a number in X100Y60?

char myArray = “X100Y60”; //should give a yes
//char myArray = “abcdefg”; //should give a no
byte lengthOfMyArray = sizeof(myArray) / sizeof(myArray[0]);
bool containsANumber = false;

void setup()
{
 Serial.begin(9600);
 for (int i = 0; i < lengthOfMyArray - 1; i++)
 {
   if (isDigit(myArray[i])) containsANumber = true;
 }
 if (containsANumber) Serial.println(“Yes”); else  Serial.println(“No”);
 } //setup

void loop()
{

} //loop

So I would do

void serialEvent() {
   serialData = Serial.readString();
   char myArray[] = serialData;
   if (myArray == True) {
      //run code here
   }
}

?

So I would do

No

How can myArray ever be true or false in any meaningful way ?

Here is the code from reply #2 slightly reformaatted

// is there a number in X100Y60?

char myArray[] = "X100Y60"; //should give a yes
//char myArray[] = "abcdefg"; //should give a no
byte lengthOfMyArray = sizeof(myArray) / sizeof(myArray[0]);
bool containsANumber = false;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < lengthOfMyArray - 1; i++)
  {
    if (isDigit(myArray[i]))
    {
      containsANumber = true;
    }
  }
  if (containsANumber)
  {
    Serial.println("Yes");
  }
  else
  {
    Serial.println("No");
  }
} //setup

void loop()
{
} //loop

Note that it is containsANumber that is checked for true/false, not the array

What exactly do you want to do ? Suppose that the String does contain a number (or do you mean a digit ?) then what ? Do you need to get the values after X and Y, for instance ?

im trying to get it to only send serial port inputs to servos if they contain a number. I'm trying to control GPIO with serial ports, but no matter what I do, it always takes any command as a command to control the servos. So, I thought that if I have it so it only sends serial inputs that contain a number to the servos, I can then set it up to turn on pin 8.

chops16:
im trying to get it to only send serial port inputs to servos if they contain a number. I'm trying to control GPIO with serial ports, but no matter what I do, it always takes any command as a command to control the servos. So, I thought that if I have it so it only sends serial inputs that contain a number to the servos, I can then set it up to turn on pin 8.

So it sounds as if you don't trust the other end of the pipe, the one that sends the c# stuff in the first place, to send data of the right format?

Are you saying there's a chance that instead of sending data shaped like XnnnYnnn, it can send any old nonsense, and you need to trap the mistake?

No, it's that i want to send, for example, "laser on" to the serial of the Arduino, but whenever I do that, the arduino detects it as something that should control the servos, even though there are no numbers

It is past the time in this thread where you should have posted your full sketch so that we can see what you are doing and how

No, it's that i want to send, for example, "laser on" to the serial of the Arduino

Amounts to the same thing really, it's just that it's not an accident.

You could check each character to be an X or an L. (No need to send "Laser on"... you could send L for on and say l for off).

If it's an X, call a function to call parseInt twice to parse the numbers into valX and valY.

If it's an L or an l, turn the laser on or off.

You should be reading Robin2's serial tutorial.

This is my current code without any modifications to turn on/off GPIO

#include<Servo.h>

Servo serX;
Servo serY;

String serialData;

void setup() {

  serX.attach(11);
  serY.attach(10);
  Serial.begin(9600);
  Serial.setTimeout(10);
}

void loop() {
  //lol
}

void serialEvent() {
  serialData = Serial.readString();
  serX.write(parseDataX(serialData));
  serY.write(parseDataY(serialData));
}

int parseDataX(String data){
  data.remove(data.indexOf("Y"));
  data.remove(data.indexOf("X"), 1);
  return data.toInt();
}

int parseDataY(String data){
  data.remove(0,data.indexOf("Y") + 1);
  return data.toInt();
}

Have a look at this. (Written before you posted your code, so I haven't looked at that.)

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
} //setup

void loop()
{
  if (Serial.available() > 0)
  {
    char incomingChar;
    incomingChar = Serial.read();
    if (incomingChar == 'X') parseServoAngles();
    if (incomingChar == 'L')
    {
      digitalWrite(13, HIGH);
      Serial.println("Laser is on");
    }
    if (incomingChar == 'l')
    {
      digitalWrite(13, LOW);
      Serial.println("Laser is off");
    }

  }
} //loop

void parseServoAngles()
{
  int x = Serial.parseInt();
  Serial.println(x);
  int y = Serial.parseInt();
  Serial.println(y);
}

If I type this into the serial monitor's input line:

LX100Y60l

... I get:

Laser is on
100
60
Laser is off

jubukraa:
Have a look at this. (Written before you posted your code, so I haven't looked at that.)

void setup()

{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
} //setup

void loop()
{
  if (Serial.available() > 0)
  {
    char incomingChar;
    incomingChar = Serial.read();
    if (incomingChar == 'X') parseServoAngles();
    if (incomingChar == 'L')
    {
      digitalWrite(13, HIGH);
      Serial.println("Laser is on");
    }
    if (incomingChar == 'l')
    {
      digitalWrite(13, LOW);
      Serial.println("Laser is off");
    }

}
} //loop

void parseServoAngles()
{
  int x = Serial.parseInt();
  Serial.println(x);
  int y = Serial.parseInt();
  Serial.println(y);
}




If I type this into the serial monitor's input line:



LX100Y60l




... I get:



Laser is on
100
60
Laser is off

Thank you so much! It works!

Cool, that's good news.

You could refine it to look for an X then only parse out the x value, and look for a Y to parse the y value. That way you won't force the sending c# program to send both, and you could send x and y alone if that was a requirement.

[side note]You will notice that within about 30m of asking the actual question, you got an actual answer. There's a lesson in there somewhere....

Asking about how to see if a string contained a number wasted quite some time on forum members' parts, not to mention effort on unnecessary coding. (Wasting your own time is of course, your prerogative ;).) [/side note]