プログラミングメモ

思った通りに動いてくれて嬉しかったこと

String.Splitで文字列内の指定文字を区切り文字として配列に分割

サンプル1

'テキストデータ

Dim sampledata As String = "日時,天気,温度"

'区切り文字

Dim delimiter As Char = ","

'Sprit受け取り配列
Dim array() As String

 

array = sampledata.Split(delimiter)

 

For Each bufstr In array
    Console.WriteLine(bufstr)
Next

解説

csvファイルの読み込み時など、意外に使い道があるかと思います。

変数sampledata内の,(カンマ)を区切り文字として配列に分割します。

 

サンプル2

概要

list変数内のデータをComboBoxの一覧に表示します。

準備

WindowsフォームVBプロジェクトのフォームに

ComboBox.ComboBox1をセットしてください。

 

FormLoadイベントなどでCom()を呼び出してください。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Com()
End Sub

Sub Com()

    Dim list As String = "1,2,3,4,5,6,7,8,9,10"
    Dim arr() As String = list.Split(",")

    For Each buf In arr
        ComboBox1.Items.Add(buf)
    Next

End Sub