Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Int32.Parse(Regex.Match(some_string, @"d+").Value);
- public string GetFirstNumber(string some_string) {
- if (string.IsNullOrEmpty(some_string)) {
- return string.Empty; // You could return null to indicate no data
- }
- StringBuilder sb = new StringBuilder();
- bool found = false;
- foreach(char c in some_string){
- if(Char.IsDigit(c)){
- sb.Append(c);
- found = true;
- } else if(found){
- // If we have already found a digit,
- // and current character is not a digit, stop looping
- break;
- }
- }
- return sb.ToString();
- }
- string str = "768 - Hello World";
- int i = Int32.Parse(str.Split(' ').First());
- public int GetLeadingIntegerFromString(string myString)
- {
- if (string.IsNullOrWhiteSpace(myString))
- return;
- var parts = myString.Split('-');
- if (parts.Length < 1)
- return;
- var number = int.Parse(parts[0].Trim());
- return number;
- }
Add Comment
Please, Sign In to add comment