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.

Answers


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();

Need Your Help

LISP function to return the greater number

function parameters lisp common-lisp

I feel like an idiot asking for help for this, but I have been stuck for hours now, and I haven't been able to find anything on the internet that can help me.

JUnit verbose output

java junit robolectric

I'm using Robolectric test runner and unfortunately my test hangs (not test but somwehre in the runner). Are there any flag for JUnit, JVM to print any verbose information so I have more insights ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.