Hi all,
I am currently working on a school project and have a question, is it possible for a String Variable to take on the current value of another String Variable?
If it is possible what is the code to accomplish this?
Thanks very much,
Zeb
Hi all,
I am currently working on a school project and have a question, is it possible for a String Variable to take on the current value of another String Variable?
If it is possible what is the code to accomplish this?
Thanks very much,
Zeb
is it possible for a String Variable to take on the current value of another String Variable?
Yes
string2 = string1;
However, using Strings on the Arduino is not recommended. Consider using C style strings (zero terminated arrays of chars) instead
Hi UKHeliBob!
I have have done this and it has given me an error saying:
"invalid array assignment"
This is my code:
Initialize Variables:
char lat1[] = "0";
char rx_byte[] = "0";
String Variable = String Variable:
lat1 = rx_byte;
Do you now why this might be?
Thanks
Zeb
......I have also tried:
char lat1 = rx_byte;
and this gives an error saying "warning: invalid conversion from 'char*' to 'char' [-fpermissive] char lat1 = rx_byte;"
Your Original Post did not make the necessary distinction between the String (capital S) class and cstrings.
Your code example is using cstrings which are char arrays terminated with a NULL character '\0'
char lat1[] = "0";
char rx_byte[] = "0";
and you cannot make one cstring equal another with code like
lat1 = rx_byte
You can use the strcpy() function to copy the contents of one cstring into another cstring. However you MUST have created the receiving cstring to be long enough to hold the new data.
Note that it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. cstrings are the proper thing to use.
...R
I have have done this and it has given me an error saying:
"invalid array assignment"
If only you had posted your code in the first place then it would have been obvious that you are using strings not Strings
As suggested, try using strcopy() to copy the strings
However, what use is an array of strings that has only on entry as in your
char lat1[] = "0";
char rx_byte[] = "0";
I think that it's time that you elaborate about your project. Looks like you're reading data from a GPS. Lattitude and longitude are, to my knowledge, not single characters so your character arrays are already incorrect and will cause you code show undefined behaviour when you copy more that one character.
Hi all,
Sorry for making it unclear what I was doing in my first post!
I will definitely try using strcpy() to copy the strings.
And yes that is what I am trying to do sterretje!
Thanks all for sharing your knowledge and helping me!
Zeb
Hi all!
I have successfully used the strcpy command!
Now I am trying to Serial.print the string variable if the string variable includes the word "lat".
This is the code I have uploaded:
char lat1[] = "lat123.666666";
void setup() {
Serial.begin (9600);
}
void loop() {
if (lat1 == "lat"){
Serial.print (lat1);
}
delay (500);
}
I can successfully uploaded the code, but when I look at the Serial Monitor there is no data there.
Do you know why this might be? My guess is that it is something to do with this line *** if (lat1 == "lat"){ ***
Thanks very much
Zeb
I have successfully used the strcpy command!
Good job delving into the cstring functions.
Another one you can use for prefix matching is strncmp.
int strncmp ( const char * str1, const char * str2, size_t num );
Compare characters of two strings
Compares up to num characters of the C string str1 to those of the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
Replace
if (lat1 == "lat"){
with
if (strncmp(lat1, "lat", 3) == 0){
Hi cattledog!
It's working! Thank you very much!
One question though, what does the == 0) mean/do?
if (strncmp(lat1, "lat", 3) == 0){
Thanks again,
Zeb
One question though, what does the == 0) mean/do?
strncmp() returns an integer value based on the outcome of the comparison.
When the return value of the function is == 0, then the two strings are identical.
See this reference for more details about the return values from strncmp().
OK thanks cattledog that's great!
Thanks again everyone for all your help!
Zeb