Java

Javaで長い文字列を...で省略

More than 5 years have passed since last update.

RailsのActionView::Helpers::TextHelper#excerpt的なのを探していたらCommonsLangにありました。

StringUtils (Commons Lang 2.6 API)

public static String abbreviate(String str,

int maxWidth)

    Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

If str is less than maxWidth characters long, return it.
Else abbreviate it to (substring(str, 0, max-3) + "...").
If maxWidth is less than 4, throw an IllegalArgumentException.
In no case will it return a String of length greater than maxWidth.

StringUtils.abbreviate(null, *) = null
StringUtils.abbreviate("", 4) = ""
StringUtils.abbreviate("abcdefg", 6) = "abc..."
StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
StringUtils.abbreviate("abcdefg", 4) = "a..."
StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException

Parameters:
str - the String to check, may be null
maxWidth - maximum length of result String, must be at least 4
Returns:
abbreviated String, null if null String input
Throws:
IllegalArgumentException - if the width is too small
Since:
2.0

public static String abbreviate(String str,

int offset,
int maxWidth)

    Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "...is the time for..."

Works like abbreviate(String, int), but allows you to specify a "left edge" offset. Note that this left edge is not necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear somewhere in the result.

In no case will it return a String of length greater than maxWidth.

StringUtils.abbreviate(null, *, *) = null
StringUtils.abbreviate("", 0, 4) = ""
StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..."
StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..."
StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException
StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException

Parameters:
str - the String to check, may be null
offset - left edge of source String
maxWidth - maximum length of result String, must be at least 4
Returns:
abbreviated String, null if null String input
Throws:
IllegalArgumentException - if the width is too small
Since:
2.0

public static String abbreviateMiddle(String str,

String middle,
int length)

    Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.

This abbreviation only occurs if the following criteria is met:

Neither the String for abbreviation nor the replacement String are null or empty
The length to truncate to is less than the length of the supplied String
The length to truncate to is greater than 0
The abbreviated String will have enough room for the length supplied replacement String and the first and last characters of the supplied String for abbreviation

Otherwise, the returned String will be the same as the supplied String for abbreviation.

StringUtils.abbreviateMiddle(null, null, 0) = null
StringUtils.abbreviateMiddle("abc", null, 0) = "abc"
StringUtils.abbreviateMiddle("abc", ".", 0) = "abc"
StringUtils.abbreviateMiddle("abc", ".", 3) = "abc"
StringUtils.abbreviateMiddle("abcdef", ".", 4) = "ab.f"

Parameters:
str - the String to abbreviate, may be null
middle - the String to replace the middle characters with, may be null
length - the length to abbreviate str to.
Returns:
the abbreviated String if the above criteria is met, or the original String supplied for abbreviation.
Since:
2.5