Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have string variable on my VB.net which came from Stored Procedure, after put it on variable , i want to make the values have specified Space.

the rule is like this

enter image description here

the number on the image mean spaces,

i already do some trial and error like this :

                Dim words As String() = receiptText.Split(" ")
                Dim word As String
                Dim XMLBuild As New StringBuilder
                XMLBuild.Append(Space(9))
                For Each word In words

                    XMLBuild.Append(word)
                    XMLBuild.Append(Space(9))

                Next

but the result is

{         PLEASE         KEEP         THIS         RECEIPT         AS         VALID         PROOF         OF         PAYMENT         }

not like the rule that i should do. is there any string function i shold use ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
160 views
Welcome To Ask or Share your Answers For Others

1 Answer

You split your input string to every space, I am not sure that this is a good approach because the person names, city address and other fields have spaces in between words, however if you want to format yours input in a fixed space area then I will give you a general approach, at least for the first line of your example above

' This is how the first line appears in the string array after the space splitting'
Dim parts = new string() {"BL", "TH", ":", "NO",  "REF.", ":", "1234567890", "R.WAHYUDIYOINO,IR"}

' This array contains the spaces reserved for each word in the string array above'
Dim spaces = new integer () {3,9,2,17,10,2,17,2,28 }

' Now loop on the strings and format them as indicated by the spaces'
' Attention, the value for space include the string itself'
Dim sb = new StringBuilder()
Dim x As INteger = 0
For each s in parts
    sb.Append(s.PadRight(spaces(x)))
    x += 1
Next
sb.AppendLine()
' To check the result (it is useful only if the output window has fixed space font)'
sb.Append("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
Console.WriteLine(sb.ToString())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...