Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Replace numbers after the . with other numbers

Replace numbers after the . with other numbers

Scheduled Pinned Locked Moved C#
helpcsharptutorial
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dan sh

    Is this a CSV file? I would say use File.ReadAllLines and then use Regular Expressions to find the decimal numbers. Then using the Matches collection you can replace digits. You can also use String.Replace method alone to do this.

    K Offline
    K Offline
    ksaw123
    wrote on last edited by
    #5

    Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,

    D A P 3 Replies Last reply
    0
    • K ksaw123

      Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #6

      Quick and dirty way:

        string sFile = System.IO.File.ReadAllText(@"c:\\test.txt");
        string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])";
      
        foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) {
          sFile = sFile.Replace(oMatch.Value.ToString(), ".0000");
        }
        System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
      

      I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.

      H OriginalGriffO 2 Replies Last reply
      0
      • K ksaw123

        Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #7

        Hi, Do you need to know the fractional part of the number or will a simple text replacement suffice? i.e. If string starts with "-9999." then string = "-9999.0000" Alan.

        1 Reply Last reply
        0
        • D dan sh

          Quick and dirty way:

            string sFile = System.IO.File.ReadAllText(@"c:\\test.txt");
            string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])";
          
            foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) {
              sFile = sFile.Replace(oMatch.Value.ToString(), ".0000");
            }
            System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
          

          I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.

          H Offline
          H Offline
          himanshu2561
          wrote on last edited by
          #8

          d@nish wrote:

          string sRegEx = @"(\.[0-9][0-9][0-9][0-9])";

          you can write

          string sRegEx = @"(\.[0-9]{4})";

          :)

          himanshu

          1 Reply Last reply
          0
          • K ksaw123

            Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #9

            The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.

            Regex regex = new Regex(
            @"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
            RegexOptions.IgnoreCase
            | RegexOptions.Multiline
            | RegexOptions.IgnorePatternWhitespace
            | RegexOptions.Compiled
            );

            You can then do a replace on this using the pattern:

            ${Start}${Part}.0000

            This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            modified on Tuesday, July 7, 2009 6:44 AM

            D OriginalGriffO 2 Replies Last reply
            0
            • D dan sh

              Quick and dirty way:

                string sFile = System.IO.File.ReadAllText(@"c:\\test.txt");
                string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])";
              
                foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) {
                  sFile = sFile.Replace(oMatch.Value.ToString(), ".0000");
                }
                System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
              

              I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #10

              Or better:

                      string sFile = System.IO.File.ReadAllText(@"c:\\test.txt");
                      Regex r = new Regex(@"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])");      
                      sFile = r.Replace(sFile, ".0000");      
                      System.IO.File.WriteAllText(@"c:\\test.txt", sFile);            
              

              He still needs to find a better regex though!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • P Pete OHanlon

                The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.

                Regex regex = new Regex(
                @"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
                RegexOptions.IgnoreCase
                | RegexOptions.Multiline
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
                );

                You can then do a replace on this using the pattern:

                ${Start}${Part}.0000

                This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                modified on Tuesday, July 7, 2009 6:44 AM

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #11

                Pete O'Hanlon wrote:

                Regex regex = new Regex( @"(((?-?)(?\d*))(\.\d{4}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );

                "parsing "(((?-?)(?\d*))(\.\d{4}))" - Unrecognized grouping construct." This is the exception while using your regex.

                P 1 Reply Last reply
                0
                • D dan sh

                  Pete O'Hanlon wrote:

                  Regex regex = new Regex( @"(((?-?)(?\d*))(\.\d{4}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );

                  "parsing "(((?-?)(?\d*))(\.\d{4}))" - Unrecognized grouping construct." This is the exception while using your regex.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #12

                  See the modification above. I didn't have "Encode HTML tags when pasting" set, so the group tags were hidden

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.

                    Regex regex = new Regex(
                    @"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
                    RegexOptions.IgnoreCase
                    | RegexOptions.Multiline
                    | RegexOptions.IgnorePatternWhitespace
                    | RegexOptions.Compiled
                    );

                    You can then do a replace on this using the pattern:

                    ${Start}${Part}.0000

                    This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    modified on Tuesday, July 7, 2009 6:44 AM

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #13

                    Pete O'Hanlon wrote:

                    This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like.

                    Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.

                    Regex regex = new Regex( @"(((?<Start>-?)(?<Part>\d+))(\.\d{4}))",
                    RegexOptions.IgnoreCase |
                    RegexOptions.Multiline |
                    RegexOptions.IgnorePatternWhitespace |
                    RegexOptions.Compiled );

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    P 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Pete O'Hanlon wrote:

                      This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like.

                      Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.

                      Regex regex = new Regex( @"(((?<Start>-?)(?<Part>\d+))(\.\d{4}))",
                      RegexOptions.IgnoreCase |
                      RegexOptions.Multiline |
                      RegexOptions.IgnorePatternWhitespace |
                      RegexOptions.Compiled );

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #14

                      OriginalGriff wrote:

                      Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.

                      Good spot.

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups