晴耕雨読

working in the fields on fine days and reading books on rainy days

VB.NET で文字列操作

VB.NET で文字列操作する関数・メソッドの一覧

System

先頭・末尾の文字列検索 (StartsWith, EndsWith)

Dim str As String = "test.csv"
Console.WriteLine(str.StartsWith("hoge")) ' => False
Console.WriteLine(str.EndsWith("csv"))    ' => True

指定文字列で連結 (Join)

Dim strings() As String = {"abc", "DEF", "123"}
Console.WriteLine(String.Join("|", strings)) ' => abc|DEF|123

右寄せ左寄せ (PadLeft, PadRight)

Dim s1 As String = "123"
Console.WriteLine(s1.PadLeft(7, "0"c))  ' => 0000123
Console.WriteLine(s1.PadRight(7, "."c)) ' => 123....

前後の空白を削除 (Trim)

Dim s1 As String = " " + vbTab + " 123 " + vbCrLf + " "
Console.WriteLine(s1.Trim)      ' => "123"
Console.WriteLine(s1.TrimStart) ' => "123 \n "
Console.WriteLine(s1.TrimEnd)   ' => " \t 123"

部分文字列 (Substring)

Dim s1 As String = "abcdEFG"
Console.WriteLine(s1(2))              ' => c
Console.WriteLine(s1.Substring(1, 3)) ' => bcd

文字列が空か判定

Dim s1 As String = Nothing
Console.WriteLine(String.IsNullOrEmpty(s1)) ' => True
s1 = String.Empty
Console.WriteLine(String.IsNullOrEmpty(s1)) ' => True
s1 = "a"
Console.WriteLine(String.IsNullOrEmpty(s1)) ' => False

文字列の検索 (IndexOf)

Dim str As String = "abcDEFghiDEF"
Console.WriteLine(str.IndexOf("def"))     ' => -1
Console.WriteLine(str.IndexOf("DEF"))     ' => 3
Console.WriteLine(str.LastIndexOf("DEF")) ' => 9

文字列の削除 (Remove)

Dim s1 = "abcDEFghi"
Console.WriteLine(s1.Remove(3, 3)) ' => abcghi

文字列の挿入 (Insert)

Dim s1 = "abcghi"
Console.WriteLine(s1.Insert(3, "DEF")) ' => abcDEFghi

文字列の置換 (Replace)

Dim s1 = "abcDEFghiZZZ123abc"
Console.WriteLine(s1.Replace("abc", "あいうえお")) ' => あいうえおDEFghiZZZ123あいうえお

文字列を含むか (Contains)

Dim str As String = "abcDEFghi"
Console.WriteLine(str.Contains("def")) ' => False
Console.WriteLine(str.Contains("DEF")) ' => True

文字列整形 (Format)

Dim i1 As Integer = 1234567
Console.WriteLine(String.Format("{0}", i1))    ' => 1234567
Console.WriteLine(String.Format("{0:N}", i1))  ' => 1,234,567.00
Console.WriteLine(String.Format("{0:N0}", i1)) ' => 1,234,567
Console.WriteLine(String.Format("{0:C}", i1))  ' => ï¿¥1,234,567
Console.WriteLine(String.Format("{0:D9}", i1)) ' => 001234567

文字列分割 (Split)

Dim str As String = "abc,def,,123,ZZZ"
Dim result() As String = str.Split(",")
Console.WriteLine(String.Join("|", result)) ' => abc|def||123|ZZZ


System.Text

エンコーディング

Dim utf8 = Encoding.UTF8
Dim s1 As String = "こんにちは"
Dim b1() As Byte = utf8.GetBytes(s1)

Console.WriteLine(BitConverter.ToString(b1))
' => E3-81-93-E3-82-93-E3-81-AB-E3-81-A1-E3-81-AF

Console.WriteLine(utf8.GetString(b1))
' => こんにちは

可変の文字列バッファ (StringBuilder)

Dim sb1 As New StringBuilder("0")
For index = 1 To 10
    sb1.Append(", " + CStr(index))
Next
Console.WriteLine(sb1) ' => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10


System.Text.RegularExpressions

正規表現で検索とグループ

Dim s1 As String = "026-123-4567"
Console.WriteLine(Regex.IsMatch(s1, "\d{3}-\d{3}-\d{4}")) ' => True

Dim m As Match = Regex.Match(s1, "(\d{3})-(\d{3})-(\d{4})")
Console.WriteLine("index: {0}, match: {1}", m.Groups(1).Index, m.Groups(1).Value) ' => 026
Console.WriteLine("index: {0}, match: {1}", m.Groups(2).Index, m.Groups(2).Value) ' => 123
Console.WriteLine("index: {0}, match: {1}", m.Groups(3).Index, m.Groups(3).Value) ' => 4567

正規表現で置換

Dim s1 As String = "<1>2<3>4"
Console.WriteLine(Regex.Replace(s1, "<(\d)>", "{$1}")) ' => {1}2{3}4