我有一個字符串,我們稱它爲「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)) –