constantlearner
constantlearner

Reputation: 5247

removal of repeated string

I have a string something like

JNDI Locations eis/FileAdapter,eis/FileAdapter used by composite 
HelloWorld1.0.jar are not available in the 
destination domain. 

eis/FileAdapter,eis/FileAdapter is occuring twice. I want it to be formatted as

 JNDI Locations eis/FileAdapter used by composite 
    HelloWorld1.0.jar are not available in the 
    destination domain. 

I tried below thing

String[ ] missingAdapters =((textMissingAdapterList.item(0)).getNodeValue().trim().split(","));
 missingAdapters.get(0) 

but i am missing second part any better way to handle this?

Upvotes: 2

Views: 102

Answers (2)

8192K
8192K

Reputation: 5290

That should do it:

String[] missingAdapters = ((textMissingAdapterList.item(0)).getNodeValue().trim().split(","));

String result = missingAdapters[0] + " " + missingAdapters[1].split(" ", 2)[1];

assuming there is no space in this double string you want to leave out.

Upvotes: 0

jlordo
jlordo

Reputation: 37853

In your comment below the question you confirm, that the duplicates will alway be conencted via a comma. Using this information, this should work (for most cases):

String replaceCustomDuplicates(String str) {
    if (str.indexOf(",") < 0) {
        return str; // nothing to do
    }
    StringBuilder result = new StringBuilder(str.length());
    for (String token : str.split(" ", -1)) {
        if (token.indexOf(",") > 0) {
            String[] parts = token.split(",");
            if (parts.length == 2 && parts[0].equals(parts[1])) {
                token = parts[0];
            }
        } 
        result.append(token + " ");
    }
    return result.delete(result.length() - 1, result.length()).toString();
}

a little demo with your example:

String str = "JNDI Locations eis/FileAdapter,eis/FileAdapter used by composite";
System.out.println(str);
str = replaceCustomDuplicates(str);
System.out.println(str);

Previous errors fixed

Upvotes: 2

Related Questions