我想在form字段中输入[1,2,3,4,5]这样的数组,并使用该form值作为bubblesort函数对数组进行排序的参数。如何在flutter中实现呢?
我们的字符串:
final hi = "[1,2,3,4,5]";
用于删除方括号的正则表达式:
final regex = RegExp(r'([\[\]])');
在将其替换为空后,我们的字符串没有任何括号:
final justNumbers = hi.replaceAll(regex, ''); // 1,2,3,4,5
我们的字符串列表,以逗号分隔:
List<String> strings = justNumbers.split(',');
现在我们将字符串解析为整数(tryParse
用于使它返回null
而不是引发异常):
List<int> numbers = strings.map((e) => int.tryParse(e)).toList();
最后
void main() {
final hi = "[1,2,3,4,5]";
final regex = RegExp(r'([\[\]])');
final justNumbers = hi.replaceAll(regex, '');
List<String> strings = justNumbers.split(',');
List<int> numbers = strings.map((e) => int.tryParse(e)).toList();
print(numbers);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句