我在中间使用StringUtils.abbreviateMiddle(string, middle, targetLength);来缩写字符串,但是使用这种方法可以切断单词。是否有一种方式,它将只切断字符串的中间位置最近的空间的字符串?
public static String shortenStringMiddle(String string, String middle, int targetLength) {
targetLength = Math.abs(targetLength);
if (string != null && string.length() > targetLength) {
return StringUtils.abbreviateMiddle(string, middle, targetLength);
}
else {
return string;
}
}如果适用于此文本,请输入:
东南亚鱼类和虾养殖疾病控制-诊断和畜牧业技术:SEAFDEC会议记录-OIE研讨会-东南亚鱼类和虾养殖疾病控制研讨会-诊断和畜牧业技术,2001年12月4日至6日,菲律宾伊洛伊洛市
System.out.println(StringUtils.abbreviateMiddle("Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Proceedings of the SEAFDEC-OIE Seminar-Workshop on Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques, 4-6 December 2001, Iloilo City, Philippines", " … ", 220));输出:
东南亚鱼类和虾养殖业的病害防治--诊断和畜牧业技术:Procee…2001年12月4日至6日,菲律宾伊洛伊洛市,东南亚业-诊断和畜牧业技术
“诉讼”一词被切断到Procee和。
我的理想输出是:
东南亚鱼类和虾养殖业的病害控制-诊断和畜牧业技术:…综述东南亚水产养殖-诊断和畜牧业技术,2001年12月4日至6日,菲律宾伊洛伊洛市
我在这里搜索过,类似的问题只涉及缩写字符串,在结尾加上省略号。
发布于 2015-08-07 03:17:31
下面是基于正则表达式的另一个实现:
public static String abbreviateMiddle(String input, String middle, int length) {
if (input != null && input.length() > length) {
int half = (length - middle.length()) / 2;
Pattern pattern = Pattern.compile(
"^(.{" + half + ",}?)" + "\\b.*\\b" + "(.{" + half + ",}?)$");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
return matcher.group(1) + middle + matcher.group(2);
}
}
return input;
}对于示例文本,请执行以下调用
System.out.println(abbreviateMiddle(exampleText, " ... ", 220));将产生
东南亚鱼类和虾养殖疾病控制-诊断和畜牧业技术:会议记录.东南亚水产养殖-诊断和畜牧业技术,2001年12月4日至6日,菲律宾伊洛伊洛市
发布于 2015-08-07 02:28:21
这是一次艰难的尝试。显然,对于字符串不包含足够的空格等的情况,您可以实现更干净的回退方法,但是作为一个可以开始的基础,它应该做得很好。
public static String abbreviateMiddle(String input, String middle, int targetLength) {
if (input == null || input.length() <= targetLength) {
return input;
}
int inputLength = input.length();
int halfTargetLength = (targetLength - middle.length()) / 2;
int startLastSpace = input.substring(0, halfTargetLength).lastIndexOf(" ");
int endFirstSpace = input.indexOf(" ", inputLength - halfTargetLength);
if (startLastSpace != -1 || endFirstSpace != -1) {
return input.substring(0, startLastSpace)
+ middle
+ input.substring(endFirstSpace + 1, inputLength);
}
return input;
}对于示例文本,请执行以下调用
System.out.println(abbreviateMiddle(exampleText, " ... ", 240));会回来
东南亚鱼类和虾养殖疾病控制-诊断和畜牧业技术:会议记录.东南亚水产养殖-诊断和畜牧业技术,2001年12月4日至6日,菲律宾伊洛伊洛市
将targetLength设置为220将返回
东南亚鱼类和虾养殖疾病控制-诊断和畜牧业技术东南亚-诊断和畜牧业技术,2001年12月4日至6日,菲律宾伊洛伊洛市
发布于 2015-08-07 02:29:56
试试这个:
public String abbreviateMiddle(String string, String middle, int targetLength) {
String[] ssplit = string.split(" ");
String first = "";
String second = "";
for (int i = 0; i < ssplit.length; i++) {
if (first.length() < targetLength / 2) {
first += ssplit[i] + " ";
} else {
break;
}
}
for (int i = ssplit.length - 1; i >= 0; i--) {
if (second.length() < targetLength / 2) {
second = ssplit[i] + " " + second;
} else {
break;
}
}
return first + middle + second;
}如果希望结果字符串为最大长度但严格小于targetLength (而不是上面返回长度大于targetLength的最小字符串的代码),请使用
if (first.length() + ssplit[i].length() < targetLength /2)和
if (second.length() + ssplit[i].length() < targetLength / 2)而不是。
https://stackoverflow.com/questions/31868404
复制相似问题