I have created a profanity list and am riding text on a site that contains it. There is also a preview portion (which is below), that tells the user there is a word that is not accepted, and gives them the option to change the word or to continue with the submission of the post with the word blotted out (****).
What I was wondering, the list is huge... and I mean probably well over 225 words in this array. I was wondering if there would be a better way of doing this, maybe introducing a stringbuilder instead of concatenating the string continuously?
Public Function CheckCensoredList(ByVal txt As String) As String
If Len(txt) > 0 Then
Dim regx As Regex
Dim counts As Integer = 0
Dim arr() As String = {........}
For Each word In arr
regx = New Regex(word)
counts += regx.Matches(txt).Count
txt = txt.Replace(word, "<span style=""color:red;""><em>" & word & "</em></span>")
Next
Return (counts & ";" & txt)
End If
Return (txt)
End Function
Essentially, the txt variable is recreated each time the regex and replace is performed. I don't know if stringbuilder would be a better option. I know it is faster, especially at large texts. KEEP IN MIND that this "txt" can be up to 7,000 characters long (but in most cases, 500 or less).
I guess I can do a search for an indexOfAny and replace it that way. The problem with this is that it will continuously find this word over and over again, and not move on.
I can use the indexOf with an incrementing "i" until the ubound of the array, however now you are talking about the same thing, however more code, more memory usage, more processing, etc.
Would there be a better way? Comments please.