【问题标题】:Appending text between 2 specific characters of HTML在 HTML 的 2 个特定字符之间附加文本
【发布时间】:2014-07-22 03:57:18
【问题描述】:

我正在尝试从一组 HTML 字符串中过滤掉特定文本,但在 Visual Basic 中编写代码时遇到了问题。我要解决的问题是;

约翰

晚安了吗?

从此我只想附加以下内容;

约翰

你过得愉快吗?

**注意:我必须输入一些额外的空格以防止将其转换为 HTML。

如果第 9 个字符是“S”,我尝试编写一些不同的子例程来在第二个“>”和第三个“”和第二个之间附加文本如果第 9 个字符不是“S”,则为“

这是我一直在尝试的主要思想,但我无法找到正确的代码放入 Then 中。

 For Each line As String In AnalyzedText
        Dim chars() = line.ToCharArray
        If chars.Length >= 9 AndAlso chars(8) = "S"c Then
            'Not sure what would go here
        ElseIf chars.Length >= 9 AndAlso chars(8) <> "S"c Then
            'Not sure what would go here
        End If
    Next

我很抱歉我没能取得更大的进步,但老实说,我不知道从哪里开始,我已经尝试了我能想到的一切。您的任何帮助或想法将不胜感激。

【问题讨论】:

    标签: html vb.net string append


    【解决方案1】:

    我尝试了您的尝试,并从中制作了一个运行版本。但是,一旦发生更改,代码就会中断。

    如果 改为 会发生什么?长度不合适。您可以通过使用子字符串而不是硬编码长度而是使用“>”符号来解决这个问题。多次使用 IndexOf 和 LastIndexOf 与 substring 组合。

    如果标签不是两者之一会怎样?你可以抛出异常

    我建议对这类问题使用正则表达式。看看一个正则表达式。您可以使用在线正则表达式运行器对其进行测试。这是一个比我建议的更清洁的解决方案

    正则表达式 vb 教程: http://www.vb-paradise.de/index.php/Thread/34042-RegEx-Tutorial-Blutige-Anf%C3%A4nger-und-Fortgeschrittene/

    跑步者: http://www.myregextester.com/

    这里是您示例的代码。请注意,它会在我描述的条件下破裂。如果您的问题有进一步的进展,我可以再次帮助您。我希望这能让你找到正确的方向。

        Dim inputList As List(Of String) = New List(Of String)()
        Dim outputList As List(Of String) = New List(Of String)()
    
        Dim sectAsStringLengthBefore As Integer = "< A NAME=SECT1>< B>".Length
        Dim sectAsStringLengthAfter As Integer = "< /B>< /A>".Length
        Dim sectAsStringMinimumLength As Integer = sectAsStringLengthBefore + sectAsStringLengthAfter
    
        Dim sectAsNumericLengthBefore As Integer = "< A NAME=1.1.10>".Length
        Dim sectAsNumericLengthAfter As Integer = "< /A>< br>".Length
        Dim sectAsNumericMinimumLength As Integer = sectAsNumericLengthBefore + sectAsNumericLengthAfter
    
        inputList.Add("< A NAME=SECT1>< B>JOHN< /B>< /A>")
        inputList.Add("< A NAME=1.1.10>Have you had a good night?< /A>< br>")
    
        For Each line As String In inputList
            If line.Length >= sectAsStringMinimumLength AndAlso line(9) = "S"c Then
                outputList.Add(line.Substring(sectAsStringLengthBefore, line.Length - sectAsStringMinimumLength))
            ElseIf line.Length >= sectAsNumericMinimumLength AndAlso line(9) <> "S"c Then
                outputList.Add(line.Substring(sectAsNumericLengthBefore, line.Length - sectAsNumericMinimumLength))
            End If
        Next
    

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 2020-08-09
      相关资源
      最近更新 更多