Replace All Function in VB.net

2019-08-04 02:58发布

问题:

how can I replace all string in string varaiable like

dim str = "I am testing and testing and testing"

After replace with "and"

ReplaceAll(str,"and","or")

How can I replace all with case insentive not case sensative?

回答1:

Sounds like a case for regular expressions:

From http://weblogs.asp.net/jgalloway/archive/2004/02/11/71188.aspx

using System.Text.RegularExpressions;

string myString = "find Me and replace ME";
string strReplace = "me";

myString = Regex.Replace(myString, "me", strReplace, RegexOptions.IgnoreCase);


回答2:

    Dim str As String = "I am testing and testing and testing"
    str = str.Replace("and", "or")


回答3:

Despite the use of Regular Expression, as suggested by Matt and RQDQ, you can use the VB sugar, like:

Replace(expression, find, replacement, start, count, compare)

From the documentation.