Storing Blank Spaces in a Character Variable

KMoody

Member
Is it possible to store blank spaces in a character variable?

By default, if a character variable contains only blank spaces, then Progress treats the value as a blank string. For example, the following code would display "yes":

Code:
DEF VAR searchString AS CHAR.
searchString = " ".
DISPLAY (searchString = "").

I need to override this feature because I need the ability to search for values that begin with blank spaces:

Code:
/* ... */
s = queryString + " WHERE STRING(" + hSortColumn:NAME + ") BEGINS """ + searchString + """ BY " + hSortColumn:NAME.
  hPopupQuery:QUERY-PREPARE(s).   
  hPopupQuery:QUERY-OPEN.
 
Hi there,

I just tried the below code and it works fine (returns only the last 2 of the temp-table records) --- using 32 bit OE 10.2B07 on Windows 7:

Code:
DEF VAR spaceField AS CHAR NO-UNDO.
DEF TEMP-TABLE tt NO-UNDO
  FIELD dataField AS CHARACTER FORMAT "x(20)"
  INDEX iDef IS PRIMARY UNIQUE dataField.
spaceField = "   ".
 
CREATE tt.
ASSIGN tt.dataField = " test 1".
CREATE tt.
ASSIGN tt.dataField = "  test 2".
CREATE tt.
ASSIGN tt.dataField = "   test 3".
CREATE tt.
ASSIGN tt.dataField = "    test 4".
 
FOR EACH tt WHERE tt.dataField BEGINS spaceField:
  DISPLAY tt.dataField.
END.
 
The equal comparison ignores trailing blanks. Thus, “abc” is equal to “abc “. However, leading and embedded blanks are treated as characters and “ abc” is not equal to “abc”.
Considering this, the string probably gets trimmed from the right side, which in this case of course means the entire string is trimmed.

Jongpau's code still works like this, however, so BEGINS seems to work as desired:
Code:
DEF VAR spaceField AS CHAR NO-UNDO.
DEF TEMP-TABLE tt NO-UNDO
  FIELD dataField AS CHARACTER FORMAT "x(20)"
  INDEX iDef IS PRIMARY UNIQUE dataField.
spaceField = "   ". /* 3 Spaces */
CREATE tt.
ASSIGN tt.dataField = " ". /* 1 Space */
CREATE tt.
ASSIGN tt.dataField = "  ". /* 2 Spaces */
CREATE tt.
ASSIGN tt.dataField = "   ". /* 3 Spaces */
CREATE tt.
ASSIGN tt.dataField = "    ". /* 4 Spaces */
FOR EACH tt WHERE tt.dataField BEGINS spaceField:
  DISPLAY "x" tt.dataField. /* x is displayed twice */
END.

This code will yield true and false in this order, as it should.
Code:
DEF  VAR  searchString  AS CHAR.
DEF  VAR  searchString2 AS CHAR.
searchString = "  ". /* 2 Spaces */
searchString2 = " ". /* 1 Space */
DISPLAY (searchString BEGINS searchString2). /* true */
DISPLAY (searchString2 BEGINS searchString). /* false */
 
Okay, thanks! I think I was confused because this code...

Code:
DEF VAR searchString AS CHAR.
searchString = " ".
DISPLAY (searchString = "") /* true */.

...lead me to believe that a single space is always treated like a blank space, even in a BEGINS statement.
 
Back
Top