【发布时间】:2020-08-07 15:16:03
【问题描述】:
假设我有一个字符串:
a="23questions";
b="2questions3";
现在我需要从两个字符串中解析 23。如何从字符串值中提取该数字或任何数字?
【问题讨论】:
假设我有一个字符串:
a="23questions";
b="2questions3";
现在我需要从两个字符串中解析 23。如何从字符串值中提取该数字或任何数字?
【问题讨论】:
const text = "23questions";
第 1 步:使用正则表达式查找匹配项:
final intInStr = RegExp(r'\d+');
第 2 步:对结果做任何你想做的事:
void main() {
print(intInStr.allMatches(text).map((m) => m.group(0)));
}
【讨论】:
以下代码可以提取数字:
aStr = a.replaceAll(new RegExp(r'[^0-9]'),''); // '23'
您可以使用以下方法将其解析为整数:
aInt = int.parse(aStr);
【讨论】:
int intValue = int.parse(myString.replaceAll(RegExp('[^0-9]'), ''));