繁体   English   中英

查找并替换字符串中的特定字符

[英]Find and replace specific characters in a string

假设我有一个名为str的字符串变量,其中包含以下值:

~Header 1~
*Content 1*
*Content 2*

~Header 2~
*Content 1*
*Content 2*
*Content 3*

~Header 3~
*Content 1*

我想要的是将特殊字符~替换为<b>Header</b> ,将*替换为<p>Content</p>这样将导致:

<b>Header 1</b>
<p>Content 1</p>
<p>Content 2</p>

<b>Header 2</b>
<p>Content 1</p>
<p>Content 2</p>
<p>Content 3</p>

<b>Header 3</b>
<p>Content 1</p>

然后删除NewLine并将其替换为<br/>并使其仅一行。

到目前为止,我只需要删除NewLine并将其替换为<br/> ,然后使其仅一行即可。

预期结果

<b>Header 1</b><br/><p>Content 1</p><br/><p>Content 2</p><br/><br/><b>Header 2</b><br/><p>Content 1</p><br/><p>Content 2</p><br/><p>Content 3</p><br/><br/><b>Header 3</b><br/><p>Content 1</p>

我的当前代码

Dim str As String = TextBox.Text ' The String Value is inputted from TextBox with Multiline property

Dim newStr As String = Regex.Replace(str, vbLf, "<br/>")
newStr = Regex.Replace(str, vbCr, "<br/>")
MessageBox.Show(newStr)

当前结果

~Header 1~<br/>*Content 1*<br/>*Content 2*<br/><br/>~Header 2~<br/>*Content 1*<br/>*Content 2*<br/>*Content 3*<br/><br/>~Header 3~<br/>*Content 1*

有人可以帮我吗?

假设“〜”和“ *”字符始终出现在行的开头和结尾,则可以使用以下方法

  • 使用String.Split创建一个数组,其中每行str都有一个元素。
  • 循环浏览并进行替换。
  • 使用String.Join在每个项目之间使用<br>重建字符串。

     Dim str As String = "~Header~" & vbCrLf & "*content*" Dim lines() As String = str.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries) For i As Integer = 0 To lines.Length - 1 If lines(i).StartsWith("~") And lines(i).EndsWith("~") Then lines(i) = "<b>" & lines(i).Substring(1, lines(i).Length - 2) & "</b>" End If If lines(i).StartsWith("*") And lines(i).EndsWith("*") Then lines(i) = "<p>" & lines(i).Substring(1, lines(i).Length - 2) & "</p>" End If Next Dim strNew As String = String.Join("<br>", lines) 

[根据评论进行编辑]

如果要为每个空白行添加<br> ,则需要将StringSplitOptions更改为StringSplitOptions.None 为此,我们需要确切地知道哪些字符分隔了行(vbCR,vbLf,vbCrLf),如果用vbCrLf分隔行,则下面的代码应该起作用。

    Dim str As String = "~Header~" & vbCrLf & "*content*"
    Dim lines() As String = str.Split({vbCrLf}, StringSplitOptions.None)
    For i As Integer = 0 To lines.Length - 1
        If lines(i).StartsWith("~") And lines(i).EndsWith("~") Then
            lines(i) = "<b>" & lines(i).Substring(1, lines(i).Length - 2) & "</b>"
        End If

        If lines(i).StartsWith("*") And lines(i).EndsWith("*") Then
            lines(i) = "<p>" & lines(i).Substring(1, lines(i).Length - 2) & "</p>"
        End If
    Next
    Dim strNew As String = String.Join("<br>", lines)

正则表达式对于如下所示很有用:

Dim output As String = Regex.Replace(input, "~([^~]*)~", "<b>$1</b>")
output = Regex.Replace(output, "\*([^*]*)\*", "<p>$1</p>")
output = Regex.Replace(output, "\r?\n", "<br/>")

如果可以使用单个模式进行整个操作,但是您需要为它提供一个客户MatchEvaluator方法,它将更加复杂。 因此,只要您不介意每次通过多个模式(使用不同的替换项)运行它,它就会变得越来越简单。

您可以使用Regex发挥自己的优势。

Function ReplaceSpecial(ByVal text As String) As String
    text = Regex.Replace(text, "^~([^~]+?)~", "<b>$1</b>", RegexOptions.Multiline)
    text = Regex.Replace(text, "^\*([^*]+?)\*", "<p>$1</p>", RegexOptions.Multiline)
    Return text
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM