我有一个字符串,我们称它为“S”,它可以是最多8位数的日志,并且我想将它对齐到一个字符串中,然后在一个文本文件中向右8空格(chr(32))VB.NET特定的字符串格式
Ex。 (我把下划线的例子来标记空白。
S="1234" should result in "____1234"
S="444444" should result in "__444444"
S="abc" should result in "_____abc"
为此,我会写下面的代码
Public Function feld(ByVal S As String, Optional I As Integer = 8) As String
Dim lenS As Integer = Strings.Len(S)
Dim vorS As Integer = I - lenS
Dim rez As String = ""
For x = 1 To vorS
rez += Strings.Chr(32)
Next
rez += S
Return rez
End Function
有没有更优雅的方式来做到这一点?
似乎是[String.PadLeft]的作业(https://msdn.microsoft.com/en-us/library/system.string.padleft(V = vs.110)的.aspx )(或PadRight) – Sehnsucht
S.PadLeft(8,“_”c)) –