Microsoft KB Archive/27631

= Fixed String Initializing, Space/Null Bytes, LEN, and RTRIM$ =

Article ID: 27631

Article Last Modified on 11/21/2006



This article was previously published under Q27631



SUMMARY
An uninitialized fixed-length string is filled with null bytes. When you initialize a fixed-length string to be equal to &quot;&quot; (the &quot;null string&quot;), the string will be filled with blank bytes (spaces). This behavior of fixed-length strings differs from variable-length strings, and may affect your program's use of functions such as RTRIM$ in ways you don't expect (see code examples below).

Note that a null byte has an ASCII value of zero, the same as is returned by the CHR$(0) function, whereas a blank byte (space) has an ASCII value of 32, the same as is returned by the CHR$(32) function.

The length shown by the LEN function for a fixed-length string x will always be the fixed size n determined in the DIM x AS STRING * n statement.

This information applies to Microsoft QuickBasic versions 4.0, 4.0b, and 4.5, to Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2, to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2, and to Microsoft Visual Basic programming system version 1.0 for Windows.



MORE INFORMATION
Note that fixed-length strings are not supported in QuickBasic versions 3.0 and earlier.

Note that LEN(RTRIM$(x)) returns the actual length of just the non-null, nonblank contents of x if x is an INITIALIZED fixed-length string. (This is because RTRIM$ actually returns a variable-length string.) If x is an UNINITIALIZED fixed-length string, LEN(RTRIM$(x)) always returns the fixed, dimensioned length, because RTRIM$ is ignored in this case.

Example 1
The following code example demonstrates the difference between assigned and unassigned fixed-length strings: DIM s AS STRING * 10                         'initially nulls PRINT LEN(s),LEN(RTRIM$(s)),ASC(RIGHT$(s,1)) '10,10 and 0 (null) s = &quot;&quot;                                       'pad with spaces PRINT LEN(s),LEN(RTRIM$(s)),ASC(RIGHT$(s,1)) '10,0 and 32 (space)

Example 2
In the following sample program, A is a fixed-length string with a length of 10 bytes, and B$ is a variable-length string. Both A and B$ are assigned a string of ten blank characters (ASCII value 32). DIM a AS STRING * 10 B$ = SPACE$(10) PRINT LEN(RTRIM$(a)) ' Prints 10 since fixed string is uninitialized. PRINT LEN(RTRIM$(b$)) ' Prints 0 for variable-length string. a = &quot;&quot; PRINT LEN(RTRIM$(a)) ' prints 0 as the length of the trimmed string ' returned by RTRIM$ since fixed string is all ' blanks. Note that the RTRIM$ function returns a variable-length string, even when a fixed-length string is its argument in parentheses. In the above program, the LEN function is operating on the variable-length string returned by the RTRIM$ function.

Example 3
The following code example illustrates one case where the incorrect assumption that fixed-length strings are automatically initialized to spaces will cause unexpected results. The INPUT statement will never get executed, unless the fixed length STRING &quot;s&quot; is initialized to all blanks: DIM s AS STRING * 10       'Desired result: wait until something entered. 's = &quot;&quot;                    'Uncomment this line for expected results. WHILE RTRIM$(s) = &quot;&quot;       'Fails first time since RTRIM$(s)=10 nulls. INPUT &quot;Input data: &quot;,s  'Never executed WEND

Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10 1.00

Keywords: KB27631

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.