Android Question Textwriter question

wolfray1972

Member
Licensed User
Longtime User
Hello,

I need to add Chr(32). Multiple time after a text. until i reach position/ Col 25.

Example:

Leaf---------------green

How do i do this with textwriter?

B4X: Copy
TextWriter1.WriteLine("Leaf" & Chr(32) "Repeat Chr(32) untill i reach col 25 or positon 25" & "Green")

I have tried with substring but it doesnt do the trick

Hope someone can help me.

Regards,

Ray
 
Untested code (I am not in front of pc)
B4X: Copy
Dim firstword as String = "First"
Dim secondword as String = "Second"
Do Until firstword.Length = 25
   firstword = firstword & chr(32)
Loop
TextWriter1.WriteLine(firstword & secondword)
 
Last edited:
Upvote 0
If you call this type of routine a lot, I would suggest using StringBuilder instead of & to create your new string. Since strings are immutable, each & creates a new string. It can wreak havoc on memory consumption and the garbage collector.
 
Upvote 0
Untested code (I am not in front of pc)
B4X: Copy
Dim firstword as String = "First"
Dim secondword as String = "Second"
Do Until firstword.Length = 25
   firstword = firstword & chr(32)
Loop
TextWriter1.WriteLine(firstword & secondword)
Test;

B4X: Copy
    Dim Leaf As String = "Leaf"
    Dim green As String = "Green"
    Dim space As String
    For i = 1 To 25 - Leaf.Length
        space = space & Chr(32)
    Next
    Log($"${Leaf}${space}${green}"$)

1616570659083.png
 
Upvote 0
B4X: Copy
Private Sub Button1_Click
    Dim Leaf As String = "Leaf"
    Dim green As String = "Green"
    Log($"${Leaf}${Repeat(Chr(32), 25-Leaf.Length)}${green}"$)
End Sub

Private Sub Repeat(c As Char, n As Int) As String
    Dim Result As String
    For i = 1 To n
        Result = Result & c
    Next
    Return Result
End Sub
 
Upvote 0
Another way: you define a costant string which is made by 25 chars (spaces, underscoees, whatever).
Then you simply concatenate your first word with a properly calculated substring of the costant string and finally your second word.
Smart strings ss in post #5 are even better than concatenation
 
Upvote 0
Back
Top