Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String comparison issue 1

Status
Not open for further replies.

Jaheel22

Technical User
Jul 14, 2004
84
US
In VB6, in immediate window of Visual Studo 6.0, i type this

strA = "1200"
strB = "800"
? strA > strB
False

Why it's returng false ?

Thanks much
 
Because you are comparing them as strings. During string comparisons, the first characters are compared, then the second and so forth.

Try this...

Code:
strA = "1200"
strB = " 800"
? strA > strB

or

Code:
strA = "1200"
strB = "0800"
? strA > strB

Done this way, you will get the result you expect.

-George

"the screen with the little boxes in the window." - Moron
 
These are strings and string comparisons are done alphabetically from the first character of the string. If you want to compare values convert to number using VAL before comparing

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
To further illustrate....

Code:
strA = "Apple"
strB = "Banana"
? strA > strB

What would you expect the result to be?

-George

"the screen with the little boxes in the window." - Moron
 
One more elaboration: what you are doing is a so-called "ASCII sort" comparison. Here's an ASCII table: Strings are sorted in the order that the characters occur in this table.

Bob
 
Therefore, You might want to try this:

strA = "1200"
strB = "800"

? CDec(strA) > CDec(strB)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top