Boolean from string.

Hi All

Can anyone help me out please what i need to do is change characters in a string from a serial port : at the moment all I'm doing is assigning a string for example String myString = "my string of information " ;

The string can be anything and may contain a ( + ) sign

i want check if the string contains a word or letter or symbol for example .. if (the string contains a +){ do something here...} else {do this} but I'm not sure how to go about looking for part of a string

Help appreciated.

There are strings and there are Strings. The first is a NULL terminated array of characters. The second is an instance of a class.

How to determine if the string/String contains a character, or string of characters, depends on whether you are referring to a string or a String.

You used both in your post, so it isn't clear which you are referring to.

Post some code to make it clear.

Thank you for a fast reply

my reference String is String dataString = "<test> :test: + 000123" ;

it’s a instance of a class

There is plenty of documentation on the String class: http://arduino.cc/en/Reference/StringObject 15 seconds of reading should lead you to the indexOf() function.

Thank you so much for pointing me in the right direction you was correct 15 seconds is all was ... been looking for 1 hour on this

Appreciated

Didn't get it with this explanation :(

Just want to convert a string into bool. The string contains 'true' or 'True' or 'TRUE' and the same with 'false'.

Found on stackoverflow something like this (added it at the end of my code):

public static class StringExtensions
{
    public static bool ToBoolean(this string value)
    {
        switch (value.ToLower())
        {
            case "true":
                return true;
            case "TRUE":
                return true;
            case "True":
                return true;
            case "false":
                return false;
            case "FALSE":
                return false;
            case "False":
                return false;
            default:
                throw new InvalidCastException("You can't cast that value to a bool!");
        }
    }
}

And then tryed this:

sw = Convert.ToBoolean(LEDmsg.substring(sw_1, sw_2));

Then there is an error the Convert isn't declared :(

you can't do a switch with non numeric values.

use nested if, possibly with && and ||

if ((value == String("true")) || (value == String("TRUE")) {...}

ideally don't use the String class and I would suggest to study Serial Input Basics to handle this

J-M-L: you can't do a switch with non numeric values.

use nested if, possibly with && and ||

if ((value == String("true")) || (value == String("TRUE")) {...}

Thank you for fast response. That means there is no function that could do the same like this with numbers?

toInt()

If you're sure that the string can only contain true of false in a variety of cases, use toUpperCase and check whether the first character is 'T'.

What are you doing that makes this conversion necessary? i.e. why put such a value into a String object to start with?

wildbill:
What are you doing that makes this conversion necessary? i.e. why put such a value into a String object to start with?

I’m using node-red to send to values via mqtt to my ESP’s. All messages that reach my ESPs are strings :frowning:

Here is an example:

{"speed":200,"color":{"r":255,"g":188,"b":200,"a":1},"switch":false,"effect":1}

wildbill:
If you’re sure that the string can only contain true of false in a variety of cases, use toUpperCase and check whether the first character is ‘T’.

I can make sure to use ONLY “true” and “false” as input with everything lowercase charackters. Is there a solution as well?

nicedevil: Thank you for fast response. That means there is no function that could do the same like this with numbers?

toInt()

there is no "toBool()" function... but really using the String class for this is a pretty bad use of the scarce memory you have on your arduino.

@nicedevil why did you hijack a thread from 2011 to ask your question...

you should have opened your own thread with the specifics of your question.

J-M-L: @nicedevil why did you hijack a thread from 2011 to ask your question...

you should have opened your own thread with the specifics of your question.

Thought it is better than opening a new thread if the issue is the same ;)

In my latest experience I will be flamed while not searching the forum for same issues and posting in these threads. So now it seems to be wrong as well... these days of internet :D

well 2011 is a long time ago, and you have shown you had done some research - you would have been forgiven on that one :)

really don't use the String class, study Serial Input Basics to handle this. that's the best scalable way forward, for example leveraging example 2, you could try this from the Serial console - set at 115200 bauds and with a line feed as end character

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void recvWithEndMarker()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData()
{
  if (newData == true) {
    Serial.print(F("YOU ENTERED '"));
    if (!strncasecmp(receivedChars, "true", 4)) {
      Serial.println(F("TRUE'"));
    } else if (!strncasecmp(receivedChars, "false", 5)) {
      Serial.println(F("FALSE'"));
    } else {
      Serial.print(receivedChars);
      Serial.println(F("'... SHAME ON YOU"));
    }

    newData = false;
  }
}



void setup()
{
  Serial.begin(115200);
  Serial.println("ENTER TRUE OR FALSE");
}

void loop()
{
  recvWithEndMarker();
  showNewData();
}

In my latest experience I will be flamed while not searching the forum for same issues and posting in these threads. So now it seems to be wrong as well

There is a World of difference between not searching for an answer before posting and adding to an old thread. I for one would not have flamed you for starting a new topic on the subject, perhaps with a reference to the old one but stating your current problem