Connecting Tech Pros Worldwide Forums | Help | Site Map

How to remove commas in a string in C# ??

Member
 
Join Date: Jan 2007
Posts: 45
#1: May 23 '07
Hello all,

I am entangled in a difficult situation. I wish to remove commas in a string ( for example : 1,20,000 ) that is coming as an output from another system.
I will have to process the output in my C# application. I will have to save 1,20,000 as 120000 in SQL2K database.

Please let me know how to remove the commas from the string . I have tried String.Replace already but it is not working.

Please help


Thanks so much !
Santosh

dip_developer's Avatar
Expert
 
Join Date: Aug 2006
Location: Asansol
Posts: 641
#2: May 23 '07

re: How to remove commas in a string in C# ??


Quote:

Originally Posted by santoshsri

Hello all,

I am entangled in a difficult situation. I wish to remove commas in a string ( for example : 1,20,000 ) that is coming as an output from another system.
I will have to process the output in my C# application. I will have to save 1,20,000 as 120000 in SQL2K database.

Please let me know how to remove the commas from the string . I have tried String.Replace already but it is not working.

Please help


Thanks so much !
Santosh

please check whether 1,20,000 is a string or not????
If it is a string then Mystring.Replace(",","") must have to work....
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: May 23 '07

re: How to remove commas in a string in C# ??


Make sure you are setting the value back.

Expand|Select|Wrap|Line Numbers
  1. string mys="1,200,000";
  2. mys=mys.Replace(",","");
  3.  
Member
 
Join Date: Jun 2007
Posts: 36
#4: Jun 11 '07

re: How to remove commas in a string in C# ??


Quote:

Originally Posted by Plater

Make sure you are setting the value back.

Expand|Select|Wrap|Line Numbers
  1. string mys="1,200,000";
  2. mys=mys.Replace(",","");
  3.  

if you are sure that some currency value is coming then use
int x = Int32.Parse(st, System.Globalization.NumberStyles.Currency);
\\\rly other optons are there for the NumberStyles. Check it out.
Newbie
 
Join Date: Sep 2009
Posts: 3
#5: Sep 23 '09

re: How to remove commas in a string in C# ??


I'm working on my first real assignment in c# where I have a csv file and i'm using StreamReader to read each line of data, and I'm pulling out two fields to compare to fields from a SQL table. My problem is it is a comma seperated file and I also have some embedded commas in quoted ("") text fields that is also seperating out each field which is causing a problem for me. I've read thru many blogs, and there has got to be some easy work around for this.

SAMPLE CSV RECORDS:
6,11,1,2,4/30/2010,6152,"FREEMAN,EDWARD J,,JR",232033,61591,P1135,ADMINISTRATIVE CLERK III,EBA,ANCHORAGE,2010,2010,6311002, , ,71172,E100,58.75,1193.52
6,11,1,3,4/30/2010,6168,"MCDONALD,PATRICIA A",311065,61948,P1135,ADMINISTRATIVE CLERK III,EBA,ANCHORAGE,2010,2010,6311003, , ,71172,E100,73.5,1143.04
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Sep 24 '09

re: How to remove commas in a string in C# ??


Best work around for reading in a CSV i've found? Use the msJET db driver. Open the csv like you would a database (see connectionstrings.com) and read it in that way.
Newbie
 
Join Date: Sep 2009
Posts: 3
#7: Sep 24 '09

re: How to remove commas in a string in C# ??


I found similar suggestions in other posts, but i was still hoping there was a setting i can add to streamreader to take care of that...but the oledbjet seems to be the best option.

Thank you.
Reply