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
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!");
}
}
}
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?
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