Guest User

Untitled

a guest
Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. Int32.Parse(Regex.Match(some_string, @"d+").Value);
  2.  
  3. public string GetFirstNumber(string some_string) {
  4.  
  5. if (string.IsNullOrEmpty(some_string)) {
  6. return string.Empty; // You could return null to indicate no data
  7. }
  8.  
  9. StringBuilder sb = new StringBuilder();
  10. bool found = false;
  11.  
  12. foreach(char c in some_string){
  13. if(Char.IsDigit(c)){
  14. sb.Append(c);
  15. found = true;
  16. } else if(found){
  17. // If we have already found a digit,
  18. // and current character is not a digit, stop looping
  19. break;
  20. }
  21. }
  22.  
  23. return sb.ToString();
  24. }
  25.  
  26. string str = "768 - Hello World";
  27. int i = Int32.Parse(str.Split(' ').First());
  28.  
  29. public int GetLeadingIntegerFromString(string myString)
  30. {
  31. if (string.IsNullOrWhiteSpace(myString))
  32. return;
  33.  
  34. var parts = myString.Split('-');
  35. if (parts.Length < 1)
  36. return;
  37.  
  38. var number = int.Parse(parts[0].Trim());
  39.  
  40. return number;
  41. }
Add Comment
Please, Sign In to add comment