首页 > 解决方案 > 来自链接/URL 的 Dart 子字符串

问题描述

我试图26189-abstract-color-background.jpg从这个字符串中获取

void main() {
  //? link string
  String s = "http://www.exampleSite.com/files/146/26189-abstract-color-background.jpg?666ff";
  // s = s.substring(s.lastIndexOf("/")+1);
  String s1 = s.substring(s.indexOf('') - 2);
  print(s1);
}

标签: stringdartsplit

解决方案


使用Uri.parse()pathSegments

 void main() {
  String s = "http://www.exampleSite.com/files/146/26189-abstract-color-background.jpg?666ff";
  var uri = Uri.parse(s);
  print(uri.pathSegments.last);
}

推荐阅读