2009/05/27(Wed) 22:51:09 編集(投稿者)
■No36357 (魔界の仮面弁士 さん) に返信
> 原始的に一文字ずつ処理していく方法しか思いつかず…。
今度はどうかな…。単純に ";#" で千切った後、必要に応じて再連結という処理方法です。
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
TextBox1.Text = "ほげ;;#ほげ;#ふがふ;;;;#が;#ぴよ;;ぴよ"
End Sub
Public Shared Function GetFields(ByVal fromString As String) As String()
If String.IsNullOrEmpty(fromString) Then
Return New String() {}
End If
'とりあえず、";#" で分割しておく
Dim fields() As String = Split(fromString, ";#")
';;# のパターンを補正
Dim list As New List(Of String)()
Dim max As Integer = UBound(fields)
For index As Integer = 0 To max
Dim s As String = fields(index)
If (s.Length - s.TrimEnd(";"c).Length) Mod 2 = 1 Then
If index = max Then
'Throw New ArgumentException("末尾に不正な[;]が存在します。", "fromString")
list.Add(s & ";")
Else
'末尾の;が奇数個なら再連結
fields(index + 1) = s & ";#" & fields(index + 1)
End If
Else
list.Add(s)
End If
Next
'最後に、";;" を ";" に置換
For n As Integer = 0 To list.Count - 1
list(n) = list(n).Replace(";;", ";")
Next
Return list.ToArray()
End Function
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
ListBox1.DataSource = GetFields(TextBox1.Text)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
End Class