0

I would like to compare two url links in flutter and see if they are similar then return a true value. An example is comparing the below links

https://stackoverflow.com/
https://stackoverflow.com/questions/ask
Kevnlan
  • 487
  • 2
  • 10
  • 31
  • you can simply compare Strings "https://stackoverflow.com/" == "https://stackoverflow.com/questions/ask" ? doThis() : doThat() – Diwyansh Nov 19 '21 at 10:38

2 Answers2

2

I would do it like this:

var link1 = 'https://stackoverflow.com/';
var link2 = 'https://stackoverflow.com/questions/ask';

print(link2.contains(link1)); => it will return true
kmtz
  • 484
  • 4
  • 14
0

This stackoverflow answer shows regex for domain name matching. You can use it like this:

  final domainRegex = RegExp(r"^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)");

  var firstUrl = 'https://stackoverflow.com/';
  var secondUrl = 'https://stackoverflow.com/questions/ask';

  var firstDomain = domainRegex.firstMatch(first_url)?.group(1);   //stackoverflow.com
  var secondDomain = domainRegex.firstMatch(second_url)?.group(1);   //stackoverflow.com

  print(firstDomain == secondDomain);  //returns true