VB.NET - 我如何将包含“text”的所有列表框项目拆分到其他列表

时间:2014-10-07 15:22:43

标签: .net vb.net list split listbox

我有listbox1,包括:

ListBox1.Items.Add("https://myweb.com/#questionsquestions/4444444/x/testxx")
ListBox1.Items.Add("https://translate.google.com/#questions/HAHAHAHA/testxx")
ListBox1.Items.Add("https://translate.google.com/#questions/HAHAHAHA/testxx")
ListBox1.Items.Add("http://stackexchange.com/")
ListBox1.Items.Add("http://stackoverflow.com/questions/23304084/how-to-remove-text-from-a-list-box")
ListBox1.Items.Add("http://stackoverflow.com/users/2227126/abdullah-kassha")
ListBox1.Items.Add("http://stackoverflow.com/questions/7860214/vb-net-split-string")
ListBox1.Items.Add("#")
ListBox1.Items.Add("http://stackoverflow.com/questions/25808080/categorize-listbox-items-by-color")
ListBox1.Items.Add("")
ListBox1.Items.Add("http://stackoverflow.com/questions/7073532/textbox-text-to-listbox-items-vb-net")
ListBox1.Items.Add("questiooonsquestions/ID_3324244/v")
ListBox1.Items.Add("cccccquestions/ID_3324244/RNA")
ListBox1.Items.Add("cccccquestions/ID_9999999/RNA")

我想在"questions/""/"之间拆分或获取文字,而不包含任何空白或重复的项目

示例图片:

http://i.imgur.com/JN6FdCs.png

3 个答案:

答案 0 :(得分:0)

您可以使用正则表达式:

Imports ystem.Text.RegularExpressions

示例:

Dim input As String = "cccccquestions/ID_9999999/RNA"
Dim re As New Regex("questions\/(.*)\/")
Dim m As Match = re.Match(input)
Dim id As String = m.Groups(1).Value 'yields "ID_9999999"

答案 1 :(得分:0)

LINQ电源和字符串方法:

Dim items = From i In listBox1.Items.Cast(Of String)()
            Let index = i.IndexOf("questions/", StringComparison.OrdinalIgnoreCase)
            Where index >= 0
            Let startIndex = index + "questions/".Length
            Let endIndex = i.IndexOf("/", startIndex)
            Where endIndex >= 0
            Select i.Substring(startIndex, endIndex - startIndex)

listBox2.Items.AddRange(items.Distinct().ToArray())

结果:

4444444
HAHAHAHA
23304084
7860214
25808080
7073532
ID_3324244
ID_9999999

答案 2 :(得分:0)

启动一个超级简单的项目,并将此函数放在主类中:

Public Function Simon(ByVal x As String) As String
        Try
            Return Split(Split(x, "questions/")(1), "/")(0)
        Catch ex As Exception
            Return ""
        End Try
End Function

另外,放一个可爱的Button并将此代码放入其中

For Each y As String In ListBox1.Items
        If Not ListBox2.Items.Contains(simon(y)) And Not simon(y) = "" Then
        Dim smo As String = simon(y)
        ListBox2.Items.Add(smo)
        End If
Next

结果:

4444444
HAHAHAHA
23304084
7860214
25808080
7073532
ID_3324244
ID_9999999