我有一个类似
的字符串JNDI Locations eis/FileAdapter,eis/FileAdapter used by composite
HelloWorld1.0.jar are not available in the
destination domain.
eis / FileAdapter,eis / FileAdapter发生两次。 我希望它被格式化为
JNDI Locations eis/FileAdapter used by composite
HelloWorld1.0.jar are not available in the
destination domain.
我试过下面的事情
String[ ] missingAdapters =((textMissingAdapterList.item(0)).getNodeValue().trim().split(","));
missingAdapters.get(0)
但我错过了第二部分处理这个问题的更好方法吗?
答案 0 :(得分:2)
在下面的评论中,您确认的问题是,副本将始终通过逗号进行识别。使用此信息,这应该有效(对于大多数情况):
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();
}
用你的例子做一个小小的演示:
String str = "JNDI Locations eis/FileAdapter,eis/FileAdapter used by composite";
System.out.println(str);
str = replaceCustomDuplicates(str);
System.out.println(str);
以前的错误已修复
答案 1 :(得分:0)
应该这样做:
String[] missingAdapters = ((textMissingAdapterList.item(0)).getNodeValue().trim().split(","));
String result = missingAdapters[0] + " " + missingAdapters[1].split(" ", 2)[1];
假设这个双字符串中没有空格你想要遗漏。