Issue in String Comparison

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.

Case 1:
String vl = (String)0.78;
if( vl < (String)5)
vl = "Zero Volt!";
Serial.println(" Vl: "+vl);
Output- Vl: Zero Volt!

Case 2:
String vl = (String)230;
if( vl < (String)5)
vl = "Zero Volt!";
Serial.println(" Vl: "+vl);
Output- Vl: Zero Volt!
Expected Output- Vl: 230

Case 3:
String vl = (String)230;
if( vl < (String)20)
vl = "Zero Volt!";
Serial.println(" Vl: "+vl);
Output- Vl: 230

And does Cast lead to data loss?
Thanks in Advance!!

Please post code that at least has some remote chance of compiling.

Also please note that String != string.

Please remember to use code tags when posting code

Why are you using Strings in the first place when you could simply compare the values

Hello, Thanks for your reply!
Here is the complete revamped code inside the code tag:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  //Case 1:
  String vl = (String)0.78;
  if ( vl < (String)5)
    vl = "Zero Volt!";
  Serial.println(" Vl: " + vl); //Output- Vl: Zero Volt!

  //Case 2:
  vl = (String)230;
  if ( vl < (String)5)
    vl = "Zero Volt!";
  Serial.println(" Vl: " + vl); //Output- Vl: Zero Volt! (But Output should be Vl: 230)

  //Case 3:
  vl = (String)230;
  if ( vl < (String)20)
    vl = "Zero Volt!";
  Serial.println(" Vl: " + vl); //Output- Vl: 230
}

void loop() {

}

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

Hi Sure, I'll re-upload the code using code tags, Thanks for sharing the guide.

The ">" (greater than) and "<" (less than) operators evaluate Strings in alphabetical order, on the first character where the two differ

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?

Please be careful when referring to strings as they are not the same as Strings and things quickly get confusing

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, Could you please post some example

Already done that.

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.

I have no idea what you mean by this

Not quite. The ASCII value of the first character where the two Strings differ is used to determine the outcome of the comparison