Created
February 10, 2022 05:22
-
-
Save yamatatsu10969/424abf3e030eb5218880aa2893d78f72 to your computer and use it in GitHub Desktop.
Full width number to half width number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _fullLengthCode = 65248; | |
extension StringExtension on String { | |
String toHalfWidthNumber() { | |
final regex = RegExp(r'^[0-9]+$'); | |
final string = runes.map<String>((rune) { | |
final char = String.fromCharCode(rune); | |
return regex.hasMatch(char) | |
? String.fromCharCode(rune - _fullLengthCode) | |
: char; | |
}); | |
return string.join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment