i have a problem, have to compare two strings which have time stamp stored in it (format=hour+min+sec) . i have used the string.compareTo(string2) function to compare but the value of it never goes to zero even when both time stamps are equal (instead 48 ,which is ascii of 0 symbol) .. the problem might be null char at the end of one of these strings, but if so why ..
these are example of the things i am using:
String newtime="";
String time="";
String check="175400"; //which here means 17hrs 54min 00sec
newtime=time+hour+minute+second; //i get hour min and sec from RTC using some functions and store it to string 'newtime'
//on serialport monitor newtime is also similar format e.g 175400
//here i compare the strings check & newtime
if(!check.compareTo(newtime))
Serial.println("Alarm"); // never turns on
////////////////////
in one of the arduino examples it says :
Caution: String comparison operators can be confusing when you're comparing numeric strings, because you're used to thinking of them as numbers, not strings. If you have to compare numbers, compare them as ints, floats, or longs, and not as Strings.
*but i think it was meant just for the comparison operators and the equals function .
I don't think this approach is going to get you where you want to go. Try using the atol() method to convert the strings to longs and then do the compare.
Comparing two Strings of ASCII digits worked just fine for me.
Exploring compareTo(), it appears that it subtracts the each character in one string from the corresponding character in the other, and returns the fist difference that's not zero. The fact that you get a number like 48 - which is much bigger than the difference between the ASCII codes for any two digits - makes me suspect that your "some functions" that return second, minute and hour from the RTC are returning characters that aren't between '0' and '9', inclusive. Testing with check = "175400" and newtime = (char)1 + "75400", check.compareTo(newtime) yields 48.
To test this theory, try adding some Serial.print()'s to show you the lengths of the two strings, and to show you what's in them, character by character. Something like,
Serial.print(newtime.length()); Serial.println();
for (int i=0;i<newtime.length();i++) {
char c = newtime.charAt(i);
Serial.print(c); Serial.print(" "); Serial.print(c,DEC);
Serial.println();
}
tmd3 ,true, i think that's a good idea to first see if there any other char coming in that string too. otherwise if the Strings are completely similar it shouldn't make any difference ,numbers or alphabets ..
tmd3 , u were right. the 'newtime' String wasn't what we expected . because the function was doing BcdtoDec . so e.g instead of 170908 we were getting 1798 (it was skipping zeros) . we added zeros and everything worked fine...
so in a nutshell, String.compareTo(string2) function does also work fine with numerical string if you have to check for equality.
I once worked programming with a guy who said he had a perfect system with every variable in strings. So I asked him about leading zeros and he was puzzled and after being shown how leading zeros work and the limitations he stated that he had no need for them. And then I asked him to sort values from 0 to 999. I still don't know if he understood why but he did shut up about his perfect system.
There is something neat you can do with numbers in text or BCD. Where bit shifting lets you multiply and divide by powers of 2, text shifting lets you do the same with any base you can put in text. Moving a decimal point is also a powerful text-math tool.