-5

let s say i have a string

String link = "www.abc.com"

now i have another string

String actualLink = "www.abc.com//www.abc.com/content/detail.html"

now I need a method to check actualLink string and remove the dulpicate part with string link for example:

public String removeDuplicate(String link, String actualLink){
     ....
     return actualLink;    //so the actualLink will become to     "www.abc.com/content/detail.html"
}

any advise? thx

sefirosu
  • 2,558
  • 7
  • 44
  • 69

4 Answers4

2
actualLink = actualLink.substring(actualLink.lastIndexOf(link));
dijkstra
  • 1,068
  • 2
  • 16
  • 39
0

String.split() if the format of your link string is fixed as "domain.com//domain.com/full/url.html"

System.out.println(actualLink.split("//")[1]);
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

It is very vague what you mean with duplicate string value. From your examples I take it you want to remove the second path segment if it is equal to the first (or perhaps multiple path segments everywhere).

I would recommend splitting the string first, and then operate on the items, e.g.

LinkedHashSet<String> items = new LinkedHashSet<String>(Arrays.asList(a.split("/")));
return StringUtils.join(items, "/");

or if just the first duplicate should be removed

String[] items = a.split("/");
if(items.length > 1 && items[0].equals(items[1])) {
   return StringUtils.join(items, "/", 1);
} else {
   return a;
}
Krumelur
  • 31,081
  • 7
  • 77
  • 119
0

Your question isn't very clear but following will work for your example:

String repl = actualLink.replaceAll("(.+?)/\\1", "$1");
// repl = www.abc.com/content/detail.html

EDIT: To make it more specific you can use:

String rep; = actualLink.replaceAll("(" + Pattern.quote(link) + ")//\\1", "$1")
// repl = www.abc.com/content/detail.html
anubhava
  • 761,203
  • 64
  • 569
  • 643