Question

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

Was it helpful?

Solution

actualLink = actualLink.substring(actualLink.lastIndexOf(link));

OTHER TIPS

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

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top