Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String.Replace() method "misbehaves" - Resolved

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,
What's worked perfectly in the VS 2012 ain't working in VS 2019 - I'm talking about the function in subject.
This code works
Code:
sRetStr.Replace(cChar2Change, "")
but this
Code:
sRetStr.Replace(cChar2Change, iCompMethod)
doesn't anymore.
iCompMethod is one of the enumerated comparison types: 0 - case-sensitive, 1 - case insensitive. (See
Just in case - screenshot:
2022_02_01_15_22_StringReplaceWontTakeStringComparisonMethod_eyrskd.jpg


What's more astounding, is that this webpage in MS Documentation has yet another overload: Replace(String, String, Boolean, CultureInfo) - four arguments, not even three!

So, what am I missing here?
Please advise!

Regards,

Ilya
 
You seem to have managed to hide pretty much all the overloads. Unfortunately, I have no idea how.

Also, Replace is a function, and returns a new string; it does not change the given string (i.e. in your case sRetStr will not be changed)

 
Solution found!
Replace() in String class indeed has these shortcomings I have described.
But Replace() in Strings has not! Therefore the following code works as expected:

Code:
If String.IsNullOrEmpty(sReplacementChars) Then ' The calling proc just wants to delete this Char from the given string - OK!
	sRetStr = Strings.Replace(sRetStr, cChar2Change, "")
Else
	sRetStr = Strings.Replace(sRetStr, cChar2Change, sReplacementChars, 1, -1, iCompMethod)
End If
(I just wonder why there's such difference between String and Strings classes... [ponder] )
Anyway, problem's resolved, case is closed.
Thank y'al! :)

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top