* added truncate and truncate-by-word String methods to help with custom formats

This commit is contained in:
Reinhard Pointner 2015-06-08 17:15:44 +00:00
parent d4a13b7eb3
commit 2bd8ffc464
1 changed files with 22 additions and 0 deletions

View File

@ -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.
*/