「VB.NET」Splitメソッドで指定した区切文字で分割するサンプル
構文
文字列.Split(区切り文字)
StringクラスのSplitメソッドを使用して、文字列を指定した区切り文字で分割します。
使用例
Module Module1
Public Sub Main()
Dim target As String = "study/skill,become:smart"
'区切文字/
Dim resA() As String = target.Split("/")
'区切文字,
Dim resB() As String = target.Split(",")
'区切文字:
Dim resC() As String = target.Split(":")
Console.WriteLine("区切文字/で分割する結果1: " & String.Join(" ", resA))
Console.WriteLine("区切文字,で分割する結果2: " & String.Join(" ", resB))
Console.WriteLine("区切文字:で分割する結果3: " & String.Join(" ", resC))
Console.ReadKey()
End Sub
End Module
Module Module1
Public Sub Main()
Dim target As String = "study/skill,become:smart"
'区切文字/
Dim resA() As String = target.Split("/")
'区切文字,
Dim resB() As String = target.Split(",")
'区切文字:
Dim resC() As String = target.Split(":")
Console.WriteLine("区切文字/で分割する結果1: " & String.Join(" ", resA))
Console.WriteLine("区切文字,で分割する結果2: " & String.Join(" ", resB))
Console.WriteLine("区切文字:で分割する結果3: " & String.Join(" ", resC))
Console.ReadKey()
End Sub
End Module
Module Module1 Public Sub Main() Dim target As String = "study/skill,become:smart" '区切文字/ Dim resA() As String = target.Split("/") '区切文字, Dim resB() As String = target.Split(",") '区切文字: Dim resC() As String = target.Split(":") Console.WriteLine("区切文字/で分割する結果1: " & String.Join(" ", resA)) Console.WriteLine("区切文字,で分割する結果2: " & String.Join(" ", resB)) Console.WriteLine("区切文字:で分割する結果3: " & String.Join(" ", resC)) Console.ReadKey() End Sub End Module
実行結果
区切文字/で分割する結果1: study skill,become:smart
区切文字,で分割する結果2: study/skill become:smart
区切文字:で分割する結果3: study/skill,become smart
区切文字/で分割する結果1: study skill,become:smart
区切文字,で分割する結果2: study/skill become:smart
区切文字:で分割する結果3: study/skill,become smart
区切文字/で分割する結果1: study skill,become:smart 区切文字,で分割する結果2: study/skill become:smart 区切文字:で分割する結果3: study/skill,become smart