Best Practice: Remove ',' from a string

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I want to remove all commas from a numerical form field.

I know that Replace(",", "") will to the job, but Replace(',', '') seems
more appropriot but does not work.

Any efficient suggestions would be appreciated.

Thank,

Mike
 
Mike said:
I want to remove all commas from a numerical form field.

I know that Replace(",", "") will to the job, but Replace(',', '') seems
more appropriot but does not work.

Replace (char, char) replaces one character with another - you want to
replace one character with nothing.
Any efficient suggestions would be appreciated.

Well, the first question is: have you checked that Replace(",", "")
isn't fast enough already?
 
Mike,

How does it not work? There are overloads of Replace that take two
characters as the arguments. Are you getting a compile time exception?

Can you show an example where it does not work?
 
Mike,

When you call the Replace method, are you storing the return value?
Calling Replace does not replace the characters on the string you call it
on, rather, it performs the operation on a new string, and returns that. My
guess is that you are not storing the return value and using that.

Hope this helps.
 
Mike,

When you call the Replace method, are you storing the return value?
Calling Replace does not replace the characters on the string you call it
on, rather, it performs the operation on a new string, and returns that. My
guess is that you are not storing the return value and using that.

Hope this helps.

Actually, no, that's not the reason it is failing. His invocation that is
failing, Replace(',','') has in invalid second parameter. '' is not a
valid char. There is no empty char, so he must use the overload that takes
two strings to replace , with nothing.
 
'' does not compile, no null character. I can use Replace(string, string) I
am simply looking for a very fast efficient way.

Thanks,

Mike

Nicholas Paldino said:
Mike,

How does it not work? There are overloads of Replace that take two
characters as the arguments. Are you getting a compile time exception?

Can you show an example where it does not work?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
I want to remove all commas from a numerical form field.

I know that Replace(",", "") will to the job, but Replace(',', '') seems
more appropriot but does not work.

Any efficient suggestions would be appreciated.

Thank,

Mike
 
Tom,

The best way to put it is what is the fastest way to strip a char or char[]
from a string.

Guess I should have said that to begin with.

Thanks,

Mike
 
Mike said:
I want to remove all commas from a numerical form field.

I know that Replace(",", "") will to the job, but Replace(',', '') seems
more appropriot but does not work.

Any efficient suggestions would be appreciated.

The fastest way is to not do it. Why are you removing the commas? If it's
part of parsing the string into a numeric value, try something like:

float theValue = Single.Parse(theString, NumberStyles.Currency)

which will accept commas, $ etc. You have many options for NumberStyles.
 
Back
Top