Web edition: Format strings

I would like to format a long string (44 numeric positions)

in VB6 I use this:

MySTR = "12345678901234567890123456789012345678901234" MySTR = Format$(MySTR, "@@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@")

There are some similar in Xojo web?

I don’t think there is a Format option for strings in Xojo. You can create your own method if you want. For your example it could be something like:

[code]Private Function FormatString(StringValue As String) As string
Dim value As String = “”
For i As Integer = 0 To 10
value = value + Mid(StringValue,i*4+1,4)
If i <> 10 Then value = value + " "
Next i

Return value
End Function[/code]

And you call it like this:

Dim MySTR As String MySTR = "12345678901234567890123456789012345678901234" MySTR = FormatString(MySTR)

Or this:

Function FormatString(StringValue As String, format as string) As string Dim rv as string = format For i as integer = 1 to len(StringValue) Rv = replace(rv, “@“, mid(stringValue, i, 1)) Next i // Remove any remaining @s rv = ReplaceAll(rv, “@“, “ “) Return rv End Function

I was thinking about this and did some tests and read some docs. I don’t have everything that I want to know, for example, what happens if the String is larger than the Format, from the docs I understand that if the string is shorter than the format and there is no character to put on a @ then a blank space will be there. Also that the format start from right to left unless the ! sign is present on the format string.

What I did is to fulfill the following conditions:

  • work right to left
  • put a blank character if there is no value left in the original string to fill the format
  • put the rest of the characters at left if there are more in the original string

Here is the code:

Function FormatString(StringValue As String, format as string) As string Dim rv As String Dim k As Integer = Len(StringValue) For i As Integer = Len(Format) DownTo 1 If Mid(Format,i,1) = "@" And k>0 Then rv = Mid(StringValue,k,1) + rv k = k - 1 Else rv = " " + rv End If Next i If k > 0 Then rv = Mid(StringValue,1,k) + rv Return rv End Function