String.Replace working in VB but not C#
The following VB code works correctly and does not flag up any errors.
strLine = strLine.Replace(strLine.LastIndexOf(","), "")
However the same C# code doesn't:
strLine = strLine.Replace(strLine.LastIndexOf(","), "");
This will not compile as it says
The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.
How come this works in VB but not in C#? and how do I fix this?
I thought it might be similar to C# string.Replace doesn't work but it implies that that code will infact complile.
Likewise with other string.Replace questions: string.Replace not working, it appears they will infact compile, whereas mine will not.
LastIndexOf returns an integer, not a string. Replace takes string, string as parameters, you're passing it int, string, hence the exception The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.
If you're just looking to remove all , use this:
strLine = strLine.Replace(",", "");
Based on your code, you may be only wanting to replace the last instance of , so try this if that's what you want:
StringBuilder sb = new StringBuilder(strLine); sb[strLine.LastIndexOf(",")] = ""; strLine = sb.ToString();