VDF-GUIdance logo



  Visual DataFlex Logo
  

Shared knowledge leads to accumulated knowledge

        Printer Friendly Page


No Files Available
Date Created: 11/13/2002
Date Updated: 11/13/2002
Author: Wil van Antwerpen
Company: Antwise Solutions
Summary:
Comparing strings padded with spaces in DataFlex might not exactly work as you would have expected as it is a little bit different from other programming languages.
You have to be aware that comparing 'abcd' to 'abcd ' always results to a (True) condition

Comparing Padded Strings


by Wil van Antwerpen

Description



Normally when you compare 2 strings to one another, you would have expected that they always have to be exactly the same in order to have the expression return true.
This might be the case for all other languages, it surely isn't the case for DataFlex.
If you compare "abcd" to "abcd " you will see that your expression returns true.

The reason of this behaviour is perfectly valid and is due to the history of DataFlex and its amazing backwards compatibility. Ascii field buffers are always returning their data to the programmer in the full stringsize.
So if you move the field Customer.Name to the string sName it will return the actual data padded with spaces until the size is reached.
If you get this value from the datadictionary using the Get Field_Current_Value then you will see that the returned data is always trimmed.

Proof of concept



In order to reproduce this feature, we have written the following small program to clearly understand how this impacts us and the programs we write:

//
// A little testprogram which shows that DataFlex does not care about padded spaces
// after a string when comparing strings.
//
//

Use dfPanel.pkg
Use dfbase.pkg

String sValue

Move "abcd       " To sValue
Showln "Comparing the string '" sValue "' with 'abcd' using the expression evaluator"
If (sValue = "abcd") Showln "  STRINGS ARE EQUAL"
Else Showln "  strings don't match"
showln ""

Move "      abcd" To sValue
Showln "Comparing the string '" sValue "' with 'abcd' using the expression evaluator"
If (sValue = "abcd") Showln "  STRINGS ARE EQUAL"
Else Showln "  strings don't match"
showln ""

Move "abcd       " To sValue
Showln "Comparing the string '" sValue "' with 'abcd' using EQ "
If sValue EQ "abcd" Showln "  STRINGS ARE EQUAL"
Else Showln "  strings don't match"
showln ""

Move "abcd " To sValue
Showln "Comparing the string '" sValue "' with 'abcd       ' using EQ"
If sValue EQ "abcd       " Showln "  STRINGS ARE EQUAL"
Else Showln "  strings don't match"
showln ""

Move "       " To sValue
Showln "Now comparing the string '" sValue "' with '' using EQ"
If sValue EQ "" Showln "  STRINGS ARE EQUAL"
Else Showln "  strings don't match"
showln ""

Send info_box "Click to QUIT"

The output of this little program is:


Comparing the string 'abcd       ' with 'abcd' using the expression evaluator
  STRINGS ARE EQUAL

Comparing the string '      abcd' with 'abcd' using the expression evaluator
  strings don't match

Comparing the string 'abcd       ' with 'abcd' using EQ 
  STRINGS ARE EQUAL

Comparing the string 'abcd ' with 'abcd       ' using EQ
  STRINGS ARE EQUAL

Now comparing the string '       ' with '' using EQ
  STRINGS ARE EQUAL



Conclusion



As a result of this anomaly you can conclude that it is not always safe to use the following bit of code to check if a string is empty.
If (sValue = "") Begin

instead Vincent from DAW advices you to check the length of the string without first trimming it.
If (length(sValue) = 0) Begin