Flutter 如何将字符串中的数字列表转换为dart中的整数列表
从文件中读取一行后,我有以下字符串:Flutter 如何将字符串中的数字列表转换为dart中的整数列表,flutter,dart,Flutter,Dart,从文件中读取一行后,我有以下字符串: "[0, 1, 2, 3, 4]" 将此字符串转换回列表的最佳方法是什么?只需执行以下步骤即可: 删除“[]”命令 夹板到线的列表 将其转换为整数列表 像这样的事情: List<int> list = value.replaceAll('[', '').replaceAll(']', '') .split(',') .map<int>((e) { return int.tryParse(e); //use
"[0, 1, 2, 3, 4]"
将此字符串转换回
列表的最佳方法是什么?只需执行以下步骤即可:
删除“[]”命令
夹板到线的列表
将其转换为整数列表
像这样的事情:
List<int> list =
value.replaceAll('[', '').replaceAll(']', '')
.split(',')
.map<int>((e) {
return int.tryParse(e); //use tryParse if you are not confirm all content is int or require other handling can also apply it here
}).toList();
您可以通过另一种替代方法将字符串列表转换为int列表
void main() {
List<String> stringList= ['1','2','3','4'];
List<int> intList = [];
stringList.map((e){
var intValue = int.tryParse(e);
intList.add(intValue!);
print(intList);
});
print(a);
}
void main(){
List stringList=['1','2','3','4'];
List intList=[];
stringList.map((e){
var intValue=int.tryParse(e);
添加(intValue!);
打印(intList);
});
印刷品(a);
}
或者使用for in循环
void main() {
List<String> stringList= ['1','2','3','4'];
List<int> intList = [];
for (var i in stringList){
int? value = int.tryParse(i);
intList.add(value!);
print(intList);
}
}
void main(){
List stringList=['1','2','3','4'];
List intList=[];
用于(stringList中的变量i){
int?值=int.tryParse(i);
添加(值!);
打印(intList);
}
}
void main() {
List<String> stringList= ['1','2','3','4'];
List<int> intList = [];
for (var i in stringList){
int? value = int.tryParse(i);
intList.add(value!);
print(intList);
}
}