When we compare two strings, Does data inside the string get compared or does the length of the strings get compared?
I want to compared data inside the string, I've attached the snippet of the code. for case 1 and case 3, I get the desired output whereas for the case 2 I'm getting the wrong output.
Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination
If the numbers are read as string values, they should be converted to numeric values instead of doing it the other way around. Converting numbers to strings in order to compare them is a malpractice.
float v1 = 0.78;
if (v1 < 5) Serial.println(F("V1 = Zero volt!"));
else
{
Serial.print(F("V1 = "));
Serial.println(v1);
}
Hi, Thanks for your reply, It's almost solved my doubt.
So if the numbers are read as string values then are they getting converted into the char datatype?
Hi,
I'm dealing with the String data type and I think according to the Arduino reference page string emphasize only on array of char whereas String deals with array of all the data types.
A string is a string, anything may be converted to a string but it is still a string. When you compare two strings which holds a textual representation of numbers, it is not the numbers that are compared but the ASCII values representing those numbers.
Hi Everyone, My main concern has been solved now I only have one doubt,
Does Cast ( casting variable or value from one data type to another) lead to data loss? And Should casting be avoided?
That would depend on the source and destination of the cast. You cannot cast a string to a number or a number to a string - both ways requires conversion of some sort.
Hi Everyone, I'd like to thanks all of you once again for your reply, my all the doubts are now solved. In a conclusion, string holds ASCII values, and during the comparison of any two strings, ASCII value of the first character in string gets compared.