Home > Enterprise >  Dart Substring from a Link/URL
Dart Substring from a Link/URL

Time:10-22

I am trying to get 26189-abstract-color-background.jpg from this string

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);
}

CodePudding user response:

Use Uri.parse() and 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);
}
  • Related