Posted By

krisdb on 06/07/09


Tagged

c-sharp


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
khouser


Get number from string


 / Published in: C#
 

  1. private int GetNumber(string str)
  2. {
  3. int length = str.Length;
  4. string output = String.Empty;
  5. int test = 0;
  6. bool err = false;
  7.  
  8. for(int i=0;i<=length;i++)
  9. {
  10. try
  11. {
  12. test = Convert.ToInt32(str.Substring(i,1));
  13. }
  14. catch
  15. {
  16. err = true;
  17. }
  18.  
  19. if (!err)
  20. output += str.Substring(i,1);
  21. else
  22. break;
  23. }
  24.  
  25. return Convert.ToInt32(output);
  26. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: misserwell on September 9, 2009

// Maybe coding as below can pass

private int GetNumber(string str) { int length = str.Length; string output = String.Empty; int test = 0; bool err = false;

    for(int i=0;i

You need to login to post a comment.