Skip to content

Instantly share code, notes, and snippets.

@kishan-dhankecha
Created October 8, 2021 05:54
Show Gist options
  • Save kishan-dhankecha/7857baa97ea36ec4c66b2c94fb7da7a9 to your computer and use it in GitHub Desktop.
Save kishan-dhankecha/7857baa97ea36ec4c66b2c94fb7da7a9 to your computer and use it in GitHub Desktop.
Extract first numbers from the String that starts with numbers.
void main() {
/// Temporary string for store number character.
var tempString = '';
/// Input string.
const string = '123sdc123s1d s sd155626sd s ds';
/// Split the input string in list of characters.
final chars = string.split('');
/// Loop through the list and check if character is number or not.
/// If the character is number then add to the [tempString].
/// Else stop the loop for checking numbers further.
for (final char in chars) {
final data = int.tryParse(char);
if (data != null) {
tempString += data.toString();
} else {
// If you want all the number from String, uncomment `continue;`
// continue;
break;
}
}
/// parse the temp string to int
final finalInt = int.parse(tempString);
print(finalInt);
/// prints => 123
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment