What same function ( find_first_not_of ) in core?

Is c++ String library to find String in String

Example :

    // string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("look for non-alphabetic characters...");

  std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

  if (found!=std::string::npos)
  {
    std::cout << "The first non-alphabetic character is " << str[found];
    std::cout << " at position " << found << '\n';
  }

  return 0;
}

Output :smile:
The first non-alphabetic character is - at position 12

How to use in core ?

@fangda01, have a look here:

http://docs.particle.io/core/firmware/#language-syntax-string-class

:smile:

2 Likes

For the time being there is no way to use this function on the Core, but if you can get your hands on a Photon it’s there already.

Otherwise, you might need to use your programming skills to whip up your own implementation of that fn() :wink:

1 Like

@peekay123 @ScruffR Thank you for teaching me ,I make function it

bool CheckString(String input){
   String in_put = input;
   String ListString[52] = {"a","b","c","d","e","f","g","h","i","j","k","l","m",
                            "n","o","p","q","r","s","t","u","v","w","x","y","z",
                            "A","B","C","D","E","F","G","H","I","J","K","L","M",
                            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
                             };
   for(int i = 0;i<52;i++){
     if(in_put.indexOf(ListString[i]) == 0){
       return true;
       break;
     }
   }
   return false;
 }

and test :smile:

 String val1 = "barcode9";
 String val2 = "Z";
 String val3 = "a";
 String val4 = "21654645";
 String val5 = "barcode";

 Serial.println(CheckString(val1));
 Serial.println(CheckString(val2));
 Serial.println(CheckString(val3));
 Serial.println(CheckString(val4));
 Serial.println(CheckString(val5));

Output :smile:

 1
 1
 1  
 0
 1

It Work

2 Likes

Good to hear, that you’ve got a sloution :+1:

Just a little hint:
If you only need to limit the allowed letters to alpha-letters, than you can make use of the knowledge of ASCII character mapping.
All the upper case letters have values beteween 0x41 (A) and 0x5A (Z) and the lower case letters range from 0x61 (a) to 0x7A (z).

If your string is less than 52 letters long, you will be quicker to test each character like if ((0x41 <= c && c <= 0x5A) || (0x61 <= c && c <= 0x7A))
And since indexOf does need some time too, you might even outperform that way with strings quite a bit longer than 52 char.

3 Likes