简体   繁体   English

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

[英]Find and replace specific characters in a string

Let's say I have a string variable called str containing the value below: 假设我有一个名为str的字符串变量,其中包含以下值:

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

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

~Header 3~
*Content 1*

What I want is to replace the special character which is the ~ with <b>Header</b> and * with <p>Content</p> so it will result in: 我想要的是将特殊字符~替换为<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>

and then remove the NewLine and replace it with <br/> and make it in just one line. 然后删除NewLine并将其替换为<br/>并使其仅一行。

So far I can just remove the NewLine and replace it with <br/> and then make it in just one line. 到目前为止,我只需要删除NewLine并将其替换为<br/> ,然后使其仅一行即可。

EXPECTED RESULT 预期结果

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

MY CURRENT CODE 我的当前代码

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)

CURRENT RESULT 当前结果

~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*

Can anybody help me please? 有人可以帮我吗?

Assuming the "~" and "*" characters always appear at the start and end of lines, you can use the following method 假设“〜”和“ *”字符始终出现在行的开头和结尾,则可以使用以下方法

  • Use String.Split to create an array with one element for each line of str . 使用String.Split创建一个数组,其中每行str都有一个元素。
  • Loop through the lines and make the substitutions. 循环浏览并进行替换。
  • Use String.Join to rebuild the string with <br> between each item. 使用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) 

[Edit in response to a comment] [根据评论进行编辑]

If you want a <br> to be added for each blank line, then we need to change the StringSplitOptions to StringSplitOptions.None . 如果要为每个空白行添加<br> ,则需要将StringSplitOptions更改为StringSplitOptions.None To do that, we need to know exactly what character or characters separate the lines (vbCR, vbLf, vbCrLf), the following code should work if the lines are separated by vbCrLf. 为此,我们需要确切地知道哪些字符分隔了行(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)

Regex would be useful for something like this: 正则表达式对于如下所示很有用:

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

If would be possible to do the whole operation with single pattern, but you'd need to provide it with a customer MatchEvaluator method and it would be more complicated. 如果可以使用单个模式进行整个操作,但是您需要为它提供一个客户MatchEvaluator方法,它将更加复杂。 So, as long as you don't mind running it through multiple patterns with a different replacement each time, it's simpler and easier. 因此,只要您不介意每次通过多个模式(使用不同的替换项)运行它,它就会变得越来越简单。

You can use Regex to your advantage. 您可以使用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