Android Question porting vb6 printf to b4x

Discussion in 'Android Questions' started by bdobaj, Jan 31, 2019.

  1. bdobaj

    bdobaj New 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"))
     
  2. DonManfred

    DonManfred Expert Licensed User

    Please use [CODE]code here...[/CODE] tags when posting code.

    [​IMG]
    [​IMG]
    [​IMG]
     
  3. MarkusR

    MarkusR Well-Known Member Licensed User

    be aware the CLRF constant in b4x is a single character!
    Code:
    Log(CRLF.Length)
    Code:
    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 StringAs 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
     
    AnandGupta likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice