Initialize a String

  • Thread starter Thread starter Jim Pockmire
  • Start date Start date
Jim,

strString = String(200, " ")

....or...

strString = String(200, Chr(32))

But why do you want to initialise a string to 200 spaces??

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Not sure yet. I need to export a fixed lenth text file that needs to be
padded with spaces beween the fields - some left justified and some right
justified.
 
Or, just to be contrary,

strString = Space(200)

or, slightly more efficient (I think)

strString = Space$(200)
 
Function setLength(strString As String, intLength As Integer)

If Len(strString) > intLength Then
setLength = Left(strString, intLength)
Else
setLength = strString & String(intLength - Len(strString), " ")
End If

End Function

Gives the flexibility if needed to establish varying lengths. If the
length of the String is longer than the length to convert to, the
original string will be truncated.
 
David C. Holley said:
Function setLength(strString As String, intLength As Integer)

If Len(strString) > intLength Then
setLength = Left(strString, intLength)
Else
setLength = strString & String(intLength - Len(strString), " ")
End If

End Function

Gives the flexibility if needed to establish varying lengths. If the
length of the String is longer than the length to convert to, the original
string will be truncated.

or

Function setLength(strString As String, intLength As Integer)

setLength = Left(strString & Space(intLength), intLength)

End Function

Tom Lake
 
But what if the trailing character should be something other than a
space? I did try a variation of the function whereby a third parameter
would be the character to add to the string. However, I realized that
there'd have to be some additional coding to check the length and such.
 
David C. Holley said:
But what if the trailing character should be something other than a space?
I did try a variation of the function whereby a third parameter would be
the character to add to the string. However, I realized that there'd have
to be some additional coding to check the length and such.

Simple change:


Function setLength(strString As String, intLength As Integer, strCharacter
As String)

setLength = Left(strString & String(intLength, strCharacter),
intLength)

End Function

Tom Lake
 
Back
Top