2

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.

2
  • What array? A string is not an array. I'm not familiar with PHP anymore, so you might want to clarify your question. Commented Nov 8, 2013 at 7:55
  • 1
    @TimSchmelter: a string is most certainly an array. Commented Nov 8, 2013 at 7:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.