我想從串列中的每個專案(字串)中洗掉前 3 個字母。
我的串列項如下所示:
{2: test1.mp4
3: test2.mp4
4: test3.mp4
10: test4.mp4
11: test5.mp4
我想從第一個專案中洗掉“{2:”,對于我想洗掉數字 空格的所有其他專案,我只有檔案名。
uj5u.com熱心網友回復:
該substring方法是您的案例的解決方案:
String text = "11: test5.mp4";
String result = text.substring(3); // test5.mp4
如果您只想洗掉側面的多余空間,請使用trim方法
String text = " test5.mp4 ";
String result = text.trim(); // test5.mp4
uj5u.com熱心網友回復:
使用 split() 而不是修剪空白并使用設定索引可能會更好。
const track = '11: test5.mp4';
final splitted = track.split(': ');
print(splitted); // [11, test5.mp4];
uj5u.com熱心網友回復:
目前,您的串列看起來如何有點不清楚。我假設,您有以下串列:
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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523295.html
標籤:安卓细绳扑列表镖
