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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santoshsri
    New Member
    • Jan 2007
    • 48

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

    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
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    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.Replac e(",","") must have to work....

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Make sure you are setting the value back.

      Code:
      string mys="1,200,000";
      mys=mys.Replace(",","");

      Comment

      • chinu
        New Member
        • Jun 2007
        • 36

        #4
        Originally posted by Plater
        Make sure you are setting the value back.

        Code:
        string mys="1,200,000";
        mys=mys.Replace(",","");
        if you are sure that some currency value is coming then use
        int x = Int32.Parse(st, System.Globaliz ation.NumberSty les.Currency);
        \\\rly other optons are there for the NumberStyles. Check it out.

        Comment

        • mzmatterafact
          New Member
          • Sep 2009
          • 3

          #5
          ignore comma's in quotes

          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,P1 135,ADMINISTRAT IVE CLERK III,EBA,ANCHORA GE,2010,2010,63 11002, , ,71172,E100,58. 75,1193.52
          6,11,1,3,4/30/2010,6168,"MCDONALD,PATRICIA A",311065,61948,P1 135,ADMINISTRAT IVE CLERK III,EBA,ANCHORA GE,2010,2010,63 11003, , ,71172,E100,73. 5,1143.04

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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 connectionstrin gs.com) and read it in that way.

            Comment

            • mzmatterafact
              New Member
              • Sep 2009
              • 3

              #7
              thank you

              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.

              Comment

              • RoopaWali
                New Member
                • Jul 2014
                • 1

                #8
                string Temp=1,200,000;
                Temporary=Temp. Trimend(',');

                Comment

                Working...