「VB.NET」PadLeftで文字列の左側を空白埋めする

2021年9月29日

構文
Public Function PadLeft (totalWidth As Integer) As String
指定された文字数になるまで左側に空白を埋め込むことで、このインスタンス内の文字を右寄せした新しい文字列を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
Sub Main()
'String型変数の定義
Dim str As String = "study"
Console.WriteLine(str + " 長さ: " + Len(str).ToString)
'文字列の左側を空白埋める
Dim res As String = str.PadLeft(8)
Console.WriteLine(res + " 長さ: " + Len(res).ToString)
Dim res2 As String = str.PadLeft(4)
Console.WriteLine(res2 + " 長さ: " + Len(res2).ToString)
Console.ReadKey()
End Sub
End Module
Module Module1 Sub Main() 'String型変数の定義 Dim str As String = "study" Console.WriteLine(str + " 長さ: " + Len(str).ToString) '文字列の左側を空白埋める Dim res As String = str.PadLeft(8) Console.WriteLine(res + " 長さ: " + Len(res).ToString) Dim res2 As String = str.PadLeft(4) Console.WriteLine(res2 + " 長さ: " + Len(res2).ToString) Console.ReadKey() End Sub End Module
Module Module1

    Sub Main()

        'String型変数の定義
        Dim str As String = "study"
        Console.WriteLine(str + " 長さ: " + Len(str).ToString)
        '文字列の左側を空白埋める
        Dim res As String = str.PadLeft(8)
        Console.WriteLine(res + " 長さ: " + Len(res).ToString)

        Dim res2 As String = str.PadLeft(4)
        Console.WriteLine(res2 + " 長さ: " + Len(res2).ToString)

        Console.ReadKey()
    End Sub


End Module

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
study 長さ: 5
study 長さ: 8
study 長さ: 5
study 長さ: 5 study 長さ: 8 study 長さ: 5
study 長さ: 5
   study 長さ: 8
study 長さ: 5

 

VB.net

Posted by arkgame