String Builder Insert

sterlingy

Active Member
Licensed User
Longtime User
I was under the impression that INSERT would allow me to insert an entire string inside another string, but it seems not to behave that way.

I'm trying to build a string that represents a score, and when the score is zero, the string is "00000". When the score is 3298, then the string would read "003298"

I converted 3298 to a string and then used Insert(2, scoreString), but this didn't work.

So how do I insert 3298 into "000000"?

-Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
I got it to work. Seems a bit convoluted. If there is a better way, please let me know.

Here are the basics:

B4X:
Dim scoreLen As Int
score = 12334
scoreTxtCon = score
scoreLen = scoreTxtCon.Length
scoreSB.Initialize
scoreSB.Append("000000")
scoreSB.Remove(6 - scoreLen , 6)
scoreSB.Append(score)
scoretxt = scoreSB.ToString

-Sterling
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Here's my penny's worth
B4X:
Dim score As String
score = 3298
Label1.Text = MakeScore(score)
   
Sub MakeScore(str As String) As String
   
   Do Until str.Length =6
      str = 0 & str
   Loop   
   Return str
   
End Sub

Cheers mj
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Do you need to use a string? if it's for display purposes, use an Int and NumberFormat.

B4X:
Dim I As Int = 3298
Log(NumberFormat(I,6,0))
 
Last edited:
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Do you need to use a string? if it's for display purposes, use an Int and NumberFormat.

Steve,

You win today's prize. The only thing I'm not too happy with is that it inserts a comma, so I get a result like 012,345

-Sterling
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at NumberFormat2
Can't give you the exact syntax, I'm not at my PC.
-------------------
Sent via Tapatalk
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…