1

I'm looking to determine if an Arduino String contains any other character than those that would be in the [number],[number]'\r''\n'where [number] could be positive or negative.

I know I can run a for down the entire string and use if(isDigit([char]) || isWhitespace([char])||[char]=='-') however, I imagine there's a better way to do this.

Could I (for example) convert the String to an array of values and search the array for any value that doesn't correspond to the characters I'm looking for? Or is there a better way to check this out?

  • ardunio gives you .indexOf(), which saves you the inner loop and is probably faster than hand-coding it. – dandavis Jul 24 '17 at 23:23
  • Ok, so I could say give me `String.indexOf([Regular expression for A-z and symbols]) and if I got an index back I could reject the string? – ATE-ENGE Jul 25 '17 at 12:29
  • yes, except for the regexp part, you need to match explicit literals – dandavis Jul 25 '17 at 18:05
  • If your string contains 2 comma separated integers, with carriage return, then you could convert the string to a C string, then use the sscanf function, using "%d,%d\n". The return value of the function will tell you if the numbers are not in the correct format. – next-hack Aug 24 '17 at 8:05
1

There is a better way, which is called regular expressions (or regexp). However, afaik these are not supported by Arduino by default.

However, I'm sure you can find some 'generic C++' class that can handle them.

For that, it's best to check on StackOverflow.com probably.

After a short check, on the Arduino forum there is a library available for regexp:

library

It is created by Nick Gammon, so all credits are for him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.