简体   繁体   中英

Find and replace specific characters in a string

Let's say I have a string variable called str containing the value below:

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

So far I can just remove the NewLine and replace it with <br/> and then make it in just one line.

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 .
  • Loop through the lines and make the substitutions.
  • Use String.Join to rebuild the string with <br> between each item.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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