How to remove non-numeric from a string but retain decimal in C#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am using Selenium(C#) on NUnit Framework and is getting a string from UI as $4850.19.
I want to compare above string with the value from backend (DB) to assert they are equal.
I am using a below method to parse my dollar amount from front-end, but the issue is that is also stripping the decimal point; and obviously the comparison with backend is failing.



Method used:



 public static string RemoveNonNumeric(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (Char.IsNumber(s[i]))
sb.Append(s[i]);
return sb.ToString();
}


How to strip out any '$' or ',' but keep '.' in the value?










share|improve this question


















  • 5





    Isn't it as simple as return s.Replace("$", String.Empty) ?

    – spender
    Nov 29 '18 at 4:05








  • 1





    Or possibly return s.Substring(1);?

    – Enigmativity
    Nov 29 '18 at 4:09











  • Those methods won't remove the ',' character, though...

    – Rufus L
    Nov 29 '18 at 13:37


















1















I am using Selenium(C#) on NUnit Framework and is getting a string from UI as $4850.19.
I want to compare above string with the value from backend (DB) to assert they are equal.
I am using a below method to parse my dollar amount from front-end, but the issue is that is also stripping the decimal point; and obviously the comparison with backend is failing.



Method used:



 public static string RemoveNonNumeric(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (Char.IsNumber(s[i]))
sb.Append(s[i]);
return sb.ToString();
}


How to strip out any '$' or ',' but keep '.' in the value?










share|improve this question


















  • 5





    Isn't it as simple as return s.Replace("$", String.Empty) ?

    – spender
    Nov 29 '18 at 4:05








  • 1





    Or possibly return s.Substring(1);?

    – Enigmativity
    Nov 29 '18 at 4:09











  • Those methods won't remove the ',' character, though...

    – Rufus L
    Nov 29 '18 at 13:37














1












1








1








I am using Selenium(C#) on NUnit Framework and is getting a string from UI as $4850.19.
I want to compare above string with the value from backend (DB) to assert they are equal.
I am using a below method to parse my dollar amount from front-end, but the issue is that is also stripping the decimal point; and obviously the comparison with backend is failing.



Method used:



 public static string RemoveNonNumeric(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (Char.IsNumber(s[i]))
sb.Append(s[i]);
return sb.ToString();
}


How to strip out any '$' or ',' but keep '.' in the value?










share|improve this question














I am using Selenium(C#) on NUnit Framework and is getting a string from UI as $4850.19.
I want to compare above string with the value from backend (DB) to assert they are equal.
I am using a below method to parse my dollar amount from front-end, but the issue is that is also stripping the decimal point; and obviously the comparison with backend is failing.



Method used:



 public static string RemoveNonNumeric(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (Char.IsNumber(s[i]))
sb.Append(s[i]);
return sb.ToString();
}


How to strip out any '$' or ',' but keep '.' in the value?







c# selenium selenium-webdriver






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '18 at 4:00









SymboCoderSymboCoder

558




558








  • 5





    Isn't it as simple as return s.Replace("$", String.Empty) ?

    – spender
    Nov 29 '18 at 4:05








  • 1





    Or possibly return s.Substring(1);?

    – Enigmativity
    Nov 29 '18 at 4:09











  • Those methods won't remove the ',' character, though...

    – Rufus L
    Nov 29 '18 at 13:37














  • 5





    Isn't it as simple as return s.Replace("$", String.Empty) ?

    – spender
    Nov 29 '18 at 4:05








  • 1





    Or possibly return s.Substring(1);?

    – Enigmativity
    Nov 29 '18 at 4:09











  • Those methods won't remove the ',' character, though...

    – Rufus L
    Nov 29 '18 at 13:37








5




5





Isn't it as simple as return s.Replace("$", String.Empty) ?

– spender
Nov 29 '18 at 4:05







Isn't it as simple as return s.Replace("$", String.Empty) ?

– spender
Nov 29 '18 at 4:05






1




1





Or possibly return s.Substring(1);?

– Enigmativity
Nov 29 '18 at 4:09





Or possibly return s.Substring(1);?

– Enigmativity
Nov 29 '18 at 4:09













Those methods won't remove the ',' character, though...

– Rufus L
Nov 29 '18 at 13:37





Those methods won't remove the ',' character, though...

– Rufus L
Nov 29 '18 at 13:37












4 Answers
4






active

oldest

votes


















5














With Reg ex it's trivial



Regex.Replace(s, "[^0-9.]", "")





share|improve this answer































    2














    You can also use the decimal.Parse method to parse a string formatted as currency into a decimal type:



    string input = "$4,850.19";
    decimal result = decimal.Parse(input, NumberStyles.Currency);

    Console.WriteLine($"{input} => {result}");


    Output:



    enter image description here






    share|improve this answer































      1














      Another way to do it if you don't want to go the decimal.Parse route is to simply return only numeric and '.' characters from the string:



      public static string RemoveNonNumeric2(string s)
      {
      return string.Concat(s?.Where(c => char.IsNumber(c) || c == '.') ?? "");
      }





      share|improve this answer































        0














        Thanks for all inputs above, I kind of figured out an easy way to handle this for now (as shown below) -



        public static string RemoveNonNumeric(string s)
        {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        if (Char.IsNumber(s[i]) || s[i] == '.')
        sb.Append(s[i]);
        return sb.ToString();
        }


        I would like to try other ways of handling this as well soon.






        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531638%2fhow-to-remove-non-numeric-from-a-string-but-retain-decimal-in-c-sharp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          With Reg ex it's trivial



          Regex.Replace(s, "[^0-9.]", "")





          share|improve this answer




























            5














            With Reg ex it's trivial



            Regex.Replace(s, "[^0-9.]", "")





            share|improve this answer


























              5












              5








              5







              With Reg ex it's trivial



              Regex.Replace(s, "[^0-9.]", "")





              share|improve this answer













              With Reg ex it's trivial



              Regex.Replace(s, "[^0-9.]", "")






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 29 '18 at 4:19









              Vadim AlekseevskyVadim Alekseevsky

              38219




              38219

























                  2














                  You can also use the decimal.Parse method to parse a string formatted as currency into a decimal type:



                  string input = "$4,850.19";
                  decimal result = decimal.Parse(input, NumberStyles.Currency);

                  Console.WriteLine($"{input} => {result}");


                  Output:



                  enter image description here






                  share|improve this answer




























                    2














                    You can also use the decimal.Parse method to parse a string formatted as currency into a decimal type:



                    string input = "$4,850.19";
                    decimal result = decimal.Parse(input, NumberStyles.Currency);

                    Console.WriteLine($"{input} => {result}");


                    Output:



                    enter image description here






                    share|improve this answer


























                      2












                      2








                      2







                      You can also use the decimal.Parse method to parse a string formatted as currency into a decimal type:



                      string input = "$4,850.19";
                      decimal result = decimal.Parse(input, NumberStyles.Currency);

                      Console.WriteLine($"{input} => {result}");


                      Output:



                      enter image description here






                      share|improve this answer













                      You can also use the decimal.Parse method to parse a string formatted as currency into a decimal type:



                      string input = "$4,850.19";
                      decimal result = decimal.Parse(input, NumberStyles.Currency);

                      Console.WriteLine($"{input} => {result}");


                      Output:



                      enter image description here







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 29 '18 at 13:36









                      Rufus LRufus L

                      19.3k31732




                      19.3k31732























                          1














                          Another way to do it if you don't want to go the decimal.Parse route is to simply return only numeric and '.' characters from the string:



                          public static string RemoveNonNumeric2(string s)
                          {
                          return string.Concat(s?.Where(c => char.IsNumber(c) || c == '.') ?? "");
                          }





                          share|improve this answer




























                            1














                            Another way to do it if you don't want to go the decimal.Parse route is to simply return only numeric and '.' characters from the string:



                            public static string RemoveNonNumeric2(string s)
                            {
                            return string.Concat(s?.Where(c => char.IsNumber(c) || c == '.') ?? "");
                            }





                            share|improve this answer


























                              1












                              1








                              1







                              Another way to do it if you don't want to go the decimal.Parse route is to simply return only numeric and '.' characters from the string:



                              public static string RemoveNonNumeric2(string s)
                              {
                              return string.Concat(s?.Where(c => char.IsNumber(c) || c == '.') ?? "");
                              }





                              share|improve this answer













                              Another way to do it if you don't want to go the decimal.Parse route is to simply return only numeric and '.' characters from the string:



                              public static string RemoveNonNumeric2(string s)
                              {
                              return string.Concat(s?.Where(c => char.IsNumber(c) || c == '.') ?? "");
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 3 '18 at 5:29









                              Rufus LRufus L

                              19.3k31732




                              19.3k31732























                                  0














                                  Thanks for all inputs above, I kind of figured out an easy way to handle this for now (as shown below) -



                                  public static string RemoveNonNumeric(string s)
                                  {
                                  StringBuilder sb = new StringBuilder();
                                  for (int i = 0; i < s.Length; i++)
                                  if (Char.IsNumber(s[i]) || s[i] == '.')
                                  sb.Append(s[i]);
                                  return sb.ToString();
                                  }


                                  I would like to try other ways of handling this as well soon.






                                  share|improve this answer




























                                    0














                                    Thanks for all inputs above, I kind of figured out an easy way to handle this for now (as shown below) -



                                    public static string RemoveNonNumeric(string s)
                                    {
                                    StringBuilder sb = new StringBuilder();
                                    for (int i = 0; i < s.Length; i++)
                                    if (Char.IsNumber(s[i]) || s[i] == '.')
                                    sb.Append(s[i]);
                                    return sb.ToString();
                                    }


                                    I would like to try other ways of handling this as well soon.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Thanks for all inputs above, I kind of figured out an easy way to handle this for now (as shown below) -



                                      public static string RemoveNonNumeric(string s)
                                      {
                                      StringBuilder sb = new StringBuilder();
                                      for (int i = 0; i < s.Length; i++)
                                      if (Char.IsNumber(s[i]) || s[i] == '.')
                                      sb.Append(s[i]);
                                      return sb.ToString();
                                      }


                                      I would like to try other ways of handling this as well soon.






                                      share|improve this answer













                                      Thanks for all inputs above, I kind of figured out an easy way to handle this for now (as shown below) -



                                      public static string RemoveNonNumeric(string s)
                                      {
                                      StringBuilder sb = new StringBuilder();
                                      for (int i = 0; i < s.Length; i++)
                                      if (Char.IsNumber(s[i]) || s[i] == '.')
                                      sb.Append(s[i]);
                                      return sb.ToString();
                                      }


                                      I would like to try other ways of handling this as well soon.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 3 '18 at 1:01









                                      SymboCoderSymboCoder

                                      558




                                      558






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531638%2fhow-to-remove-non-numeric-from-a-string-but-retain-decimal-in-c-sharp%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Contact image not getting when fetch all contact list from iPhone by CNContact

                                          count number of partitions of a set with n elements into k subsets

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks