【问题标题】:VB.NET - how i can split all listbox items that contain "text" to other listVB.NET - 我如何将所有包含“文本”的列表框项目拆分到其他列表
【发布时间】:2014-10-07 15:22:42
【问题描述】:

我的 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

【问题讨论】:

  • 到目前为止有什么尝试吗?并可能阅读split
  • @jbutler483 当然是的,我试过了,我已经将字符串列表拆分为 ComboBox,但字符串不包含空或重复的项目,但不能在列表框中执行相同的操作,这样 /ˢᵒʳʳʸ ᶠᵒʳ ᵐʸ ᵇᵃᵈ ᵉᶰᵍˡᶤˢʰ ᶤᵗˢ ᶰᵒᵗ ᵐʸ ᵐᵒᵗʰᵉʳ ˡᵃᶰᵍᵘᵃᵍᵉ
  • 它们是如何被添加到列表框中的?每当输入下一个值时,您就不能对此进行测试吗? (在 ListBox 中使用 foreach 元素?)并检查它是否已经包含该值?如果值不是“”?

标签: .net vb.net list split listbox


【解决方案1】:

启动一个超级简单的项目,把这个Function放到主类中:

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

【讨论】:

    【解决方案2】:

    你可以使用正则表达式:

    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"
    

    【讨论】:

      【解决方案3】:

      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
      

      【讨论】:

        猜你喜欢
        • 2016-05-18
        • 2011-10-27
        • 1970-01-01
        • 2020-01-14
        • 2020-09-17
        • 2020-06-27
        • 2023-04-09
        • 2014-12-25
        • 1970-01-01
        相关资源
        最近更新 更多