在字符串上给特定空间 [英] Give Specific Space on string

查看:36
本文介绍了在字符串上给特定空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 VB.net 上有一个来自存储过程的字符串变量,将它放在变量上后,我想让这些值具有指定的空间.

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 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

但结果是

{         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 ?

推荐答案

您将输入字符串拆分为每个空格,我不确定这是一个好方法,因为人名、城市地址和其他字段之间有空格话,但是如果你想在一个固定的空间区域格式化你的输入,那么我会给你一个通用的方法,至少对于上面例子的第一行

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())

这篇关于在字符串上给特定空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆