From 2bd8ffc464723c1d751fc940680ecba5340f8ff6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 8 Jun 2015 17:15:44 +0000 Subject: [PATCH] * added truncate and truncate-by-word String methods to help with custom formats --- .../format/ExpressionFormatMethods.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index 9b490734..230cdbd3 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -163,6 +163,28 @@ public class ExpressionFormatMethods { return buffer.toString(); } + public static String truncate(String self, int limit) { + if (limit >= self.length()) + return self; + + return self.substring(0, limit); + } + + public static String truncate(String self, int hardLimit, String nonWordPattern) { + if (hardLimit >= self.length()) + return self; + + int softLimit = 0; + Matcher matcher = compile(nonWordPattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS).matcher(self); + while (matcher.find()) { + if (matcher.start() > hardLimit) + break; + + softLimit = matcher.start(); + } + return truncate(self, softLimit); + } + /** * Return substring before the given pattern. */