Searching a char Variable

Hi all,

I simply want to search a char variable but I am having real trouble finding any command that will do it!

I know that you can search a String but how do you search an array?

Thanks,

Zeb

..... Or is the reason I can't find how to is because you can't? :slight_smile:

This page is a really useful resource for c string functions. I bet it will help you a lot http://www.cplusplus.com/reference/cstring/.

Hi Byrok,

That's excellent thank you!

I want to search for a character and if it can't find the character it does......

So I think that I will use something like this:

if (strpbrk(loraDataChar, "@")) {

My question is how do I turn this into does not equal instead of equals?

Thanks,

Zeb

Not familiar with strpbrk. It will probably return NULL if the character that you're looking for is not found. Below the use of strchr

if(strchr(someText, someChar) == NULL)
{
  Not found
}
else
{
  Found it
}

And you can obviously reverse the logic

if(strchr(someText, someChar) != NULL)
{
  Found it
}
else
{
  Not found
}

If you want to use the result, assign the result to a char pointer variable and test the variable for NULL.

Hi sterretje,

Unfortunately when I run this code:

    if (strchr("@", loraDataChar) == NULL) {
      
      Serial.println ("Message Received:");
      Serial.println (loraDataChar);

      /*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print("MESSAGE RECEIVED");
      display.setCursor(0, 55);
      display.print("RSSI:");
      display.print(rssi);
      display.display();
      delay (5000);
    }

It gives this error:

invalid conversion from 'char*' to 'int' [-fpermissive]

About this line of code:

    if (strchr("@", loraDataChar) == NULL) {

loraDataChar is initialized like this:

char loraDataChar[numChars]; //An Array to copy the loraData String contents to

What have I done wrong?

Thanks,

Zeb

You're currently looking for the haystack in the needle instead of the other way around.

The first argument to strchr is the haystack where you look for the needle, the second argument is the needle that you're looking for.

So you have to swap your parameters around.

PS: strchr looks for a char in a c-string, not for a c-string in a c-string.

Sorry I think I am missing something because I did both this:

char loraDataChar[numChars]; //An Array to copy the loraData String contents to
char symbol[] = "@";
if (strchr(symbol, loraDataChar) != NULL)
    {
    }
    else
    {
      Serial.println ("Message Received:");
      Serial.println (loraDataChar);

      /*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print("MESSAGE RECEIVED");
      display.setCursor(0, 55);
      display.print("RSSI:");
      display.print(rssi);
      display.display();
      delay (5000);
    }

And this:

if (strchr("@", loraDataChar) != NULL)
    {
    }
    else
    {
      Serial.println ("Message Received:");
      Serial.println (loraDataChar);

      /*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print("MESSAGE RECEIVED");
      display.setCursor(0, 55);
      display.print("RSSI:");
      display.print(rssi);
      display.display();
      delay (5000);
    }

And I get this error:
invalid conversion from 'char*' to 'int' [-fpermissive]

What am I doing wrong!!!

Thanks for your help!

Zeb

And if I do this:

    if (strchr(loraDataChar, symbol) != NULL){

or this

    if (strchr(loraDataChar, "@") != NULL){

I get this error:
invalid conversion from 'char*' to 'int' [-fpermissive]

Thanks,

Zeb

'@'

Thanks arduino_new!

Thanks to all that helped (especially sterretje) it now works!

Thanks again,

Zeb