Re: Better way of replacing?
Seems like this would be best to use string builder rather than keep assigning the string value on each replace.
Code:
Dim DirtyString As String = "//This is a : test;it's (should) replace items --- & also, be fast"
DirtyString.Cleaner()
Console.WriteLine(DirtyString)
Code:
<Runtime.CompilerServices.Extension()> _
Public Sub Cleaner(ByRef sender As String)
Dim SearchItems(,) As String = _
{ _
{"/", " "}, _
{":", " "}, _
{"'", ""}, _
{"---", "-"}, _
{"&", "And"}, _
{"(", ""}, _
{")", ""} _
}
Dim sb As New System.Text.StringBuilder(sender)
Dim Rows As Integer = SearchItems.GetLength(0) - 1
For Row As Integer = 0 To Rows
sb.Replace(SearchItems(Row, 0), SearchItems(Row, 1))
Next
sender = sb.ToString
End Sub
Re: Better way of replacing?
Why not use Regex??
--------------------
Sabrina Gage
Free Live Chat Software
Re: Better way of replacing?
Quote:
Originally Posted by
Sabrina Gage
Why not use Regex??
--------------------
Sabrina Gage
Free Live Chat Software
To be honest I believe there is no gain in using regular expressions here.
Code:
Dim DirtyString As String = "//This is a : test;it's (should) replace items --- & also, be fast"
Dim dict As New Dictionary(Of String, String)
dict.Add("/", "")
dict.Add(":", "")
dict.Add("&", "And")
Console.WriteLine(DirtyString.DictReplace(dict))
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function DictReplace( _
ByVal sender As String, _
ByVal UserDictionary As Dictionary(Of String, String)) As String
Return Regex.Replace(sender, "(" & _
String.Join("|", UserDictionary.Keys.ToArray()) & ")", _
Function(m As Match) UserDictionary(m.Value))
End Function
Hopefully someone can poke holes at this method simply because there are issues with using a dictionary.