删除列表Dart中字符串的前3个字母



我想从列表中的每个项目(字符串(中删除前3个字母。

我的Listitems如下所示:

{2: test1.mp4
3: test2.mp4
4: test3.mp4
10: test4.mp4
11: test5.mp4

我想删除";{2:"从第一个项目和每一个其他项目,我想删除数字+空格,这样我只有文件名。

substring方法是您的案例的解决方案:

String text = "11: test5.mp4";
String result = text.substring(3); //  test5.mp4

如果你只想去除侧面的多余空间,可以使用trim方法

String text = "     test5.mp4    ";
String result = text.trim(); // test5.mp4

使用split((可能会更好,而不是修剪空白并使用集合索引。

const track = '11: test5.mp4';
final splitted = track.split(': ');
print(splitted); // [11, test5.mp4];

目前,您的列表看起来有点不清楚。我假设,你有以下列表:

List<String> myList = [
"2: test1.mp4",
"3: test2.mp4",
"4: test3.mp4",
"10: test4.mp4",
"11: test5.mp4",
];

在这种情况下,不一定只删除前3个字母。可扩展的解决方案如下:

final List<String> myList = [
"2: test1.mp4",
"3: test2.mp4",
"4: test3.mp4",
"10: test4.mp4",
"11: test5.mp4",
];

//We are splitting each item at ": ", which gives us a new array with two 
//items (the number and the track name) and then we grab the last item of
//that array.
final List<String> myFormatedList = myList.map((e) => e.split(": ").last).toList();
print(myFormatedList);
//[test1.mp4, test2.mp4, test3.mp4, test4.mp4, test5.mp4]

最新更新