# Searching a char Variable

**URL:** <https://forum.arduino.cc/t/searching-a-char-variable/630338>\
**Category:** Programming\
**Created:** [January 10, 2020, 3:21am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338 "2020-01-10T03:21:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 3:21am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/1 "2020-01-10T03:21:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 3:29am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/2 "2020-01-10T03:29:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Byork](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Byork](https://forum.arduino.cc/u/Byork)\
**Post date:** [January 10, 2020, 3:58am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/3 "2020-01-10T03:58:46Z")

</div>

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/](http://www.cplusplus.com/reference/cstring/).

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 5:02am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/4 "2020-01-10T05:02:14Z")

</div>

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:

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

```

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

Thanks,

Zeb

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [January 10, 2020, 5:24am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/5 "2020-01-10T05:24:37Z")

</div>

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

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

```

And you can obviously reverse the logic

```cpp
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.

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 6:01am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/6 "2020-01-10T06:01:09Z")

</div>

Hi sterretje,

Unfortunately when I run this code:

```cpp
    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:

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

```

About this line of code:

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

```

loraDataChar is initialized like this:

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

```

What have I done wrong?

Thanks,

Zeb

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [January 10, 2020, 6:09am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/7 "2020-01-10T06:09:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 6:36am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/8 "2020-01-10T06:36:08Z")

</div>

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

```cpp
char loraDataChar[numChars]; //An Array to copy the loraData String contents to
char symbol[] = "@";

```

```cpp
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:

```cpp
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

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 6:40am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/9 "2020-01-10T06:40:06Z")

</div>

And if I do this:

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

```

or this

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

```

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

Thanks,

Zeb

---

<div class="post-metadata">

**Author:** ![arduino\_new](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@arduino\_new](https://forum.arduino.cc/u/arduino_new)\
**Post date:** [January 10, 2020, 6:43am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/10 "2020-01-10T06:43:47Z")

</div>

```cpp
'@'

```

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 6:45am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/11 "2020-01-10T06:45:36Z")

</div>

Thanks arduino\_new!

---

<div class="post-metadata">

**Author:** ![ZebH](https://avatars.discourse-cdn.com/v4/letter/z/e0b2c6/32.png) [@ZebH](https://forum.arduino.cc/u/ZebH)\
**Post date:** [January 10, 2020, 6:49am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/12 "2020-01-10T06:49:33Z")

</div>

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

Thanks again,

Zeb

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:14am UTC](https://forum.arduino.cc/t/searching-a-char-variable/630338/13 "2021-05-06T00:14:34Z")

</div>

Closed on May 6, 2021, 12:14am UTC


