String.Replace Help

AlphaStu

Member
Joined
Mar 4, 2005
Messages
8
Location
UK
Programming Experience
5-10
Hi,

I would like to be able to do the following using String.Replace, but I can't get it to work because it is case sensitive.

--------------------------------------------------------------
Private Sub Button5_Click()
Dim myStr As String
myStr = "ABcd abCD"

MsgBox(myStr.Replace("AB", "yz"))
End Sub
--------------------------------------------------------------
I would like the message box to return 'yzcd yzCD'.

Does anyone know how I can do this like I used to be able to in VB6 so it replaces all instances of the searched for string no matter what the case (ie not needing to specify AB, Ab, aB or ab in the search for part.

Many thanks in advance
Stuart
 

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
AlphaStu said:
Hi,

I would like to be able to do the following using String.Replace, but I can't get it to work because it is case sensitive.

--------------------------------------------------------------
Private Sub Button5_Click()
Dim myStr As String
myStr = "ABcd abCD"

MsgBox(myStr.Replace("AB", "yz"))
End Sub
--------------------------------------------------------------
I would like the message box to return 'yzcd yzCD'.

Does anyone know how I can do this like I used to be able to in VB6 so it replaces all instances of the searched for string no matter what the case (ie not needing to specify AB, Ab, aB or ab in the search for part.

Many thanks in advance
Stuart
your code works fine.
VB.NET:
Dim s As String = "ABy xyz"
MessageBox.Show(s.Replace("AB", "FL"))
 

AlphaStu

Member
Joined
Mar 4, 2005
Messages
8
Location
UK
Programming Experience
5-10
Thanks for your help, but perhaps the simple example was too simple.

Basically I will have some text (could be 1000's of words long). With this text I will have a function that will replace all instances of the specified word. For example I may want to replace all instances of the word 'the'. However because of the replace member of string being case sensitive it will only replace the exact word. Therefore replacing 'the' will not replace 'The' or 'THE' etc. I need to somehow get a function that will replace all instances of the specified word no matter what the case. I need something like the compare member, which has a parameter of IgnoreCase.

Any ideas anyone?

Thanks
Stuart
 

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
i think you can do that in regex
here's the example.
VB.NET:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim s As String = "The the THE tHe thE Something"
		MsgBox(Me.replacestring(s, "the", "do"))
	End Sub

	Function replacestring(ByVal repstring As String, ByVal pattern As String, ByVal replacement As String) As String
		Dim rx As Regex
		Return rx.Replace(repstring, pattern, replacement, RegexOptions.IgnoreCase)
	End Function
 

AlphaStu

Member
Joined
Mar 4, 2005
Messages
8
Location
UK
Programming Experience
5-10
Superb!! Many thanks mzim, thats exactly what I need.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Ok I tried that out but if I had 1000 'the' then i would need to have 1000 'the' in the s string, how do you solve that?
 

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
levyuk said:
Ok I tried that out but if I had 1000 'the' then i would need to have 1000 'the' in the s string, how do you solve that?
no need to put 1000 'the' in the s string just pass directly your string containing 1000 'the' and it solves the problem.
 
Top Bottom