Anna Riekic
Anna Riekic

Reputation: 738

Replace text from an array using vb.net

How do you replace text from an array using vb.net?

In PHP it you would simply create an array() and use str_replace() like so

$str     = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.';
$find    = array('Lorem', 'sit', 'elit');
$replace = str_replace($find, '', $str);

but how is it done using vb.net?

Im currently replacing like this:

With TextBox1
        : .Text = .Text.Replace("Lorem", "")
        : .Text = .Text.Replace("sit", "")
        : .Text = .Text.Replace("elit", "")
        : End With

but figured an array might be a better, faster, less resourceful option as everything that needs replacing is replaced with the same thing.

Upvotes: 2

Views: 5099

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

Write an extension method, like:

<Extension>
Public Function Replace(byval input As String, Byval replacement As String, ByVal find As String()) As String
  Dim result As String = input
  For Each item As String In find
    result = result.Replace(item, replacement)
  Next
  Return result
End Function

Or use a regex, like:

Dim value As String = "lorum ipsum dolar"
value = Regex.Replace(value, "lor|ip|do", String.Empty)

Or a combination of the two.

Upvotes: 2

Related Questions