Android Question porting vb6 printf to b4x

bdobaj

Member
Licensed User
I'm new to b4a: Can anybody port vb6 function printf to b4x

Public Function printf(mask As String, ParamArray tokens()) As String
Dim I As Long
For I = 0 To UBound(tokens)
mask = Replace$(mask, "{" & I & "}", tokens(I))
Next
printf = mask
End Function

better than this:
Public Sub printf(mask As String, tokens() As String) As String
Dim i As Short
For i = 0 To tokens.Length-1
mask = mask.Replace("{" & i & "}", tokens(i))
Next
Return mask
End Sub

vb6 : s = printf("Today is {0}. Tommorow is {1}.", "monday","tuesday")
b4a: s = printf("Today is {0}. Tommorow is {1}.", array("monday","tuesday"))


2. how can I insert CLRF like \n instring

s= "monday\n tuesday"

without doing s = "test " & CLRF & "test 2"


so I can use my printf function like:

lbl1.text = printf("Today is {0}.\nTommorow is {1}", array("monday","tuesday"))
 

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
be aware the CLRF constant in b4x is a single character!
B4X:
Log(CRLF.Length)

B4X:
Sub Test

    Log(printf("Today is {0}.\nTommorow is {1}", Array As String ("monday","tuesday")))
  
End Sub

Public Sub printf(mask As String, tokens() As String) As String
    Dim i As Short
    For i = 0 To tokens.Length-1
        mask = mask.Replace($"{${i}}"$, tokens(i)) 'Smart String Literal
    Next
    mask = mask.Replace("\n", Chr(13) & Chr(10)) 'windows new line carriage return (CR) and line feed (LF)
    Return mask
End Sub
 
Upvote 0
Top