Johan van Dam
Johan van Dam

Reputation: 47

asp.net vb replace string in string and make it bold

I'm looking for an function to replace a string in a string and only make it bold. I already got this function:

Function MakeBold(ByVal input As String, ByVal find As String) As String
    Return Regex.Replace(input, find, "<strong>" + find + "</strong>", RegexOptions.IgnoreCase)
End Function

This works but it removes the capital of a string. For example if i would run it with the string "Lorem ipsum dolor sit Dolor" and the replace part to be "dolor", it will return "Lorem ipsum dolor sit dolor". The second "dolor" loses it capital cause it's being replaced with one without a capital. How can i keep capitals in my string? So for example "DoLoR" will also still be that and not "dolor"

Upvotes: 1

Views: 1823

Answers (2)

Hans Kesting
Hans Kesting

Reputation: 39274

You can use this:

Function MakeBold(ByVal input As String, ByVal find As String) As String
    Return Regex.Replace(input, find, "<strong>$0</strong>", RegexOptions.IgnoreCase)
End Function

The $0 in the replace-pattern is subsituted with the full match.

See also here.

Upvotes: 3

Gino4nx
Gino4nx

Reputation: 64

hello what about this function:

Private Function MakeBold(allstring As String, toFind As String) As String
    Return allstring.Replace(toFind, [String].Format("<strong>{0}</strong>", toFind))
  End Function

Upvotes: 1

Related Questions