diff options
author | Jon Bratseth <bratseth@oath.com> | 2020-02-26 08:47:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-26 08:47:22 +0100 |
commit | 27a13fccf35e1ec157b7e450e94d9c9404929dc9 (patch) | |
tree | 8ad30ddd0ce283c7a8c845db8cb0ab3c2223109e /vespajlib/src/main | |
parent | 7b6f7e7462e2e6ae3d71a46d0143fc0cb6f84892 (diff) | |
parent | e4beb4bb3a014273c28e7865cf9528689186cf16 (diff) |
Merge pull request #12338 from vespa-engine/bratseth/truncate-expressions-in-messages
Trunkcate expressions in messages
Diffstat (limited to 'vespajlib/src/main')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/Text.java | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/Text.java b/vespajlib/src/main/java/com/yahoo/text/Text.java index 706fd1583a3..85b28639d89 100644 --- a/vespajlib/src/main/java/com/yahoo/text/Text.java +++ b/vespajlib/src/main/java/com/yahoo/text/Text.java @@ -174,4 +174,16 @@ public final class Text { return stripped != null ? stripped.toString() : string; } + /** + * Returns a string which is never larger than the given number of characters. + * If the string is longer than the given length it will be truncated. + * If length is 4 or less the string will be truncated to length. + * If length is longer than 4, it will be truncated at length-4 with " ..." added at the end. + */ + public static String truncate(String s, int length) { + if (s.length() <= length) return s; + if (length <= 4) return s.substring(0, length); + return s.substring(0, length - 4) + " ..."; + } + } |