java 自动省略Java中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3288716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:09:39  来源:igfitidea点击:

Automatically ellipsize a string in Java

javastring

提问by Milo Casagrande

Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries.

Java中是否有自动省略字符串的方法?仅在 Java 中,而不是其他库中。

Thanks.

谢谢。

回答by nd.

Depending on your use case, it might be useful to put the ellipsis between letters (i.e. add letters at the end to provide some context):

根据您的用例,将省略号放在字母之间可能很有用(即在末尾添加字母以提供一些上下文):

/**
 * Puts ellipses in input strings that are longer than than maxCharacters. Shorter strings or
 * null is returned unchanged.
 * @param input the input string that may be subjected to shortening
 * @param maxCharacters the maximum characters that are acceptable for the unshortended string. Must be at least 3, otherwise a string with ellipses is too long already.
 * @param charactersAfterEllipsis the number of characters that should appear after the ellipsis (0 or larger) 
 * @return the truncated string with trailing ellipses
 */
public static String ellipsize(String input, int maxCharacters, int charactersAfterEllipsis) {
  if(maxCharacters < 3) {
    throw new IllegalArgumentException("maxCharacters must be at least 3 because the ellipsis already take up 3 characters");
  }
  if(maxCharacters - 3 > charactersAfterEllipsis) {
    throw new IllegalArgumentException("charactersAfterEllipsis must be less than maxCharacters");
  }
  if (input == null || input.length() < maxCharacters) {
    return input;
  }
  return input.substring(0, maxCharacters - 3 - charactersAfterEllipsis) + "..." + input.substring(input.length() - charactersAfterEllipsis);
}

There are also more sophisticated features you might want from you ellipsis algorithm: If you need to place the text into a graphical element and you are using a proportional font, then you must measure the length of the String.

您可能还需要省略号算法提供更复杂的功能:如果您需要将文本放入图形元素并且您使用的是比例字体,那么您必须测量字符串的长度。

For Swing/AWT that would be java.awt.Font.getStringBounds. In such a case, a simple algrithm would cut the string one letter at a time and add the ellipsis, until the string fits into the limit given. If used often, the bisection method elaborated in http://www.codeproject.com/KB/cs/AutoEllipsis.aspx?msg=3278640(C#, but should be easy enough to translate to Java) may save some processor cycles.

对于 Swing/AWT,这将是java.awt.Font.getStringBounds. 在这种情况下,一个简单的算法会一次一个字母地切割字符串并添加省略号,直到字符串适合给定的限制。如果经常使用,http://www.codeproject.com/KB/cs/AutoEllipsis.aspx?msg=3278640 (C#,但应该很容易转换为 Java)中阐述的二分法可能会节省一些处理器周期。

回答by cuh

This method will return the ellipsized String:

此方法将返回椭圆形字符串:

String ellipsize(String input, int maxLength) {
    String ellip = "...";
    if (input == null || input.length() <= maxLength 
           || input.length() < ellip.length()) {
        return input;
    }
    return input.substring(0, maxLength - ellip.length()).concat(ellip);
}

回答by Sean Owen

There is not. But here's another crack at a simple method to do this.

那没有。但这里有一个简单的方法来做到这一点。

String ellipsize(String input, int maxLength) {
  if (input == null || input.length() < maxLength) {
    return input;
  }
  return input.substring(0, maxLength) + "...";
}

回答by anydoby

A oneliner which of course does not outperform the other offered solutions:

一个 oneliner 当然不会胜过其他提供的解决方案:

longString.replaceFirst("(.{5}).+(.{5})", "...")

Will leave only first and last 5 characters and put ... in the middle.

将只留下第一个和最后 5 个字符并将 ... 放在中间。

回答by paxdiablo

Sure, try this one:

当然,试试这个:

public static String ellipsise (String input, int maxLen) {
    if (input == null)
        return null;
    if ((input.length() < maxLen) || (maxLen < 3))
        return input;
    return input.substring (0, maxLen - 3) + "...";
}

This has the advantage of fixing the bug where the King's English is not used properly :-)

这样做的好处是修复了国王英语使用不当的错误:-)