Java 如何在不剪切单词的情况下在中间缩写字符串
我正在使用Java 如何在不剪切单词的情况下在中间缩写字符串,java,string,Java,String,我正在使用StringUtils.缩写middle(string,middle,targetLength)在中间缩写字符串,但使用此方法可以截断单词。是否有一种方法,使其仅在最靠近字符串中间位置的空间切断字符串 public static String shortenStringMiddle(String string, String middle, int targetLength) { targetLength = Math.abs(targetLength); if (s
StringUtils.缩写middle(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;
}
}
如果应用于此文本,则输入:
东南亚鱼虾养殖疾病控制-诊断和养殖技术:东南亚鱼虾养殖疾病控制SEADEC-OIE研讨会研讨会研讨会论文集-诊断和养殖技术,2001年12月4日至6日,菲律宾伊洛伊洛市
输出:
东南亚鱼虾养殖疾病控制-诊断和养殖技术:Procee..aculture东南亚-诊断和养殖技术,2001年12月4日至6日,菲律宾伊洛伊洛市
“程序”一词被切断为“过程”,水产养殖被切断为“文化”
我的理想输出是:
东南亚鱼虾养殖疾病控制-诊断和养殖技术:会议记录…东南亚水产养殖-诊断和养殖技术,2001年12月4日至6日,菲律宾伊洛伊洛市
我在这里搜索了SO,类似的问题只与缩写字符串在结尾添加省略号有关。要详细说明文斯的评论,您可以使用indexOf(“”),因为空格将代表一个单词的结尾(因此您不会截断一个单词)。我会仔细查看输入中“”的,然后估计两端要放多少个单词,以达到或低于字符限制(从方法中的targetLength开始),然后在所选“”实例之后或之后。我不懂java,所以我不能为您编写代码,抱歉。这里是一个粗略的尝试。显然,对于字符串不包含足够的空格等情况,您可以实现更干净的回退方法,但作为开始的基础,它应该很好
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;
}
会回来的
东南亚鱼虾养殖中的疾病控制-
诊断和饲养技术:会议记录。。。水产养殖
东南亚——诊断和饲养技术,12月4日至6日
2001年,菲律宾伊洛伊洛市
将targetLength
设置为220将返回
东南亚鱼虾养殖中的疾病控制-
诊断和饲养技术:。。。东南亚-诊断
2001年12月4日至6日,菲律宾伊洛伊洛市
试试这个:
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;
}
public String缩写中间(String String,String middle,int targetLength){
String[]ssplit=String.split(“”);
字符串优先=”;
字符串秒=”;
对于(int i=0;i=0;i--){
if(秒长()
如果希望结果字符串为最大长度但严格小于targetLength(而不是上面返回长度大于targetLength的最小字符串的代码),请使用
if(first.length()+ssplit[i].length()
及
if(second.length()+ssplit[i].length()
相反。这里是另一个基于正则表达式的实现:
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, " ... ", 240));
System.out.println(abbreviateMiddle(exampleText, " ... ", 220));
将产生
东南亚鱼虾养殖中的疾病控制-
诊断和饲养技术:会议记录。。。水产养殖
东南亚——诊断和饲养技术,12月4日至6日
2001年,菲律宾伊洛伊洛市
为什么不使用indexOf(String)
找到正确的位置?我对您遇到的问题感到困惑,更不用说,您没有向我们展示您的middle
method@VinceEmighscriblemiddle
方法取自apachecommonsStringUtils
@RobbyCornelissen是的,我不久就意识到了这一点。但是包含这些信息会很好,因为并不是每个人都使用commons(甚至在代码中使用完全限定的名称),您对string
、middle
和targetlength
?@M.Shaw的输入是什么(如果应用于此文本,请参阅输入:)中间是“…;”
是省略号,目标长度是220
。嗨,我只是好奇,从你的另一个答案中使用上面的代码有什么好处?谢谢
if (second.length() + ssplit[i].length() < targetLength / 2)
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));