Android Question [solved]add "-" in string

ronell

Well-Known Member
Licensed User
Longtime User
B4X:
dim number as string = "123"
how can i add hyphen after each char ? i need to get something like this

number = 1-2-3
 

Misterbates

Active Member
Licensed User
iterate over the string transferring chars and hyphens into a stringbuilder, then get the result with stringbuilder.tostring
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
'-'.join(number)
(Damn!! I think Python spoiled me... )

B4X:
Dim number = "123" As String
Dim output = number.CharAt(0) As String
For i = 1 To (number.Length - 1)
    output = output & "-" & number.CharAt(i)
Next
Log (output)
 
Last edited:
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
thanks !
 
Upvote 0

Misterbates

Active Member
Licensed User
Using stringbuilder (which is supposed to be faster for string appends):
B4X:
Dim number = "123" As String
Dim output as stringbuilder
output.append(number.CharAt(0))
For i = 1 To (number.Length - 1)
    output.append("-" & number.CharAt(i))
Next
Log (output.tostring)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…