微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb.net分割字符串组

如何解决vb.net分割字符串组

XML文件
这是示例文件,有2个组
(实际文件有1000个以上的组)

 <Text>You guys were the ones calling
    out and making a racket.</Text>
  
 <Text>I thought you needed help,</Text>

我希望他们在vb中像这样
如果超过2个组,则会为每个组自动创建新的文本框

[textBox1]
You guys were the ones calling
out and making a racket.

[textBox2]
I thought you needed help,

有人可以引导我吗?我想我必须通过标签找到文本
并拆分它们,但是它是重复的标签,我无法定义它们

更新
我使用此代码,并且工作正常
现在我可以拆分我想要的任何组

Dim xml = XDocument.Load("C:\Users\Administrator\Desktop\12.xml")

Dim xstr As String = xml.ToString
Dim String1 As String = "<Text>"
Dim String2 As String = "</Text>"
Dim xlist As New List(Of String)
Do
    xstr = Mid(xstr,InStr(xstr.ToLower,String1.ToLower) + String1.Length)
    xlist.Add(Mid(xstr,1,String2.ToLower) - 1))
    xstr = Mid(xstr,String2.ToLower) + String2.Length)
Loop Until InStr(xstr.ToLower,String1.ToLower) = 0

Dim i = 0

Do
    RichTextBox1.Text &= xlist(i) & Environment.NewLine
    i += 1
Loop Until i = xlist.Count

解决方法

如果是XML,则使用XML。在这里,我使用了XElement,LINQ和XML文字。希望你能从中得到一些想法。

    Dim xe As XElement
    'for production
    'xe = XElement.Load("put path here")

    'for test
    xe = <Subtitles>
             <Subtitle>
                 <Number>1</Number>
                 <Start>00:00:00,000</Start>
                 <End>00:00:02,236</End>
                 <Duration>2,236</Duration>
                 <Text>You guys were the ones calling
out and making a racket.</Text>
             </Subtitle>
             <Subtitle>
                 <Number>2</Number>
                 <Start>00:00:02,238</Start>
                 <End>00:00:03,343</End>
                 <Duration>1,105</Duration>
                 <Text>I thought you needed help,</Text>
             </Subtitle>
         </Subtitles>

    RichTextBox1.Text = (From el In xe...<Number>
                          Where el.Value = "1").FirstOrDefault.Parent.<Text>.Value

    ''or for 2

    'RichTextBox1.Text = (From el In xe...<Number>
    '                      Where el.Value = "2").FirstOrDefault.Parent.<Text>.Value

    RichTextBox1.Clear()
    Dim groups As IEnumerable(Of XElement)
    groups = xe...<Subtitle>

    For Each g As XElement In groups
        Dim s As String
        s = String.Format("Num. {0} Start:{1} End:{2}",g.<Number>.Value,g.<Start>.Value,g.<End>.Value)
        RichTextBox1.AppendText(s)
        RichTextBox1.AppendText(ControlChars.Cr)
    Next

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。