Answer by Basil Bourque for Trim a string based on the string length
tl;dr You seem to be asking for an ellipsis (…) character in the last place, when truncating. Here is a one-liner to manipulate your input string. String input = "abcdefghijkl"; String output = (...
View ArticleAnswer by Leo Droidcoder for Trim a string based on the string length
With Kotlin it is as simple as: yourString.take(10) Returns a string containing the first n characters from this string, or the entire string if this string is shorter. Documentation
View ArticleAnswer by aceminds for Trim a string based on the string length
str==null ? str : str.substring(0, Math.min(str.length(), 10)) or, str==null ? "" : str.substring(0, Math.min(str.length(), 10)) Works with null.
View ArticleAnswer by rekotc for Trim a string based on the string length
Just in case you are looking for a way to trim and keep the LAST 10 characters of a string. s = s.substring(Math.max(s.length(),10) - 10);
View ArticleAnswer by Mulki for Trim a string based on the string length
There is a StringUtils function which does this. s = StringUtils.left(s, 10) If len characters are not available, or the String is null, the String will be returned without an exception. An empty...
View ArticleAnswer by sibnick for Trim a string based on the string length
As usual nobody cares about UTF-16 surrogate pairs. See about them: What are the most common non-BMP Unicode characters in actual use? Even authors of org.apache.commons/commons-lang3 You can see...
View ArticleAnswer by MVojtkovszky for Trim a string based on the string length
Or you can just use this method in case you don't have StringUtils on hand: public static String abbreviateString(String input, int maxLength) { if (input.length() <= maxLength) return input; else...
View ArticleAnswer by H6. for Trim a string based on the string length
StringUtils.abbreviate from Apache Commons Lang library could be your friend: StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg"...
View ArticleAnswer by shift66 for Trim a string based on the string length
s = s.length() > 10 ? s.substring(0, 9) : s;
View ArticleAnswer by Stephen C for Trim a string based on the string length
s = s.substring(0, Math.min(s.length(), 10)); Using Math.min like this avoids an exception in the case where the string is already shorter than 10. Notes: The above does real trimming. If you actually...
View ArticleTrim a string based on the string length
I want to trim a string if the length exceeds 10 characters. Suppose if the string length is 12 (String s="abcdafghijkl"), then the new trimmed string will contain "abcdefgh..". How can I achieve this?
View ArticleAnswer by Mahfuzur Rahman for Trim a string based on the string length
// this is how you shorten the length of the string with ..// add following method to your classprivate String abbreviate(String s){ if(s.length() <= 10) return s; return s.substring(0, 8) +".." ;}
View ArticleAnswer by Snostorp for Trim a string based on the string length
A great compact way, without having to use a third party library, would be to use the ternary operator (?:) as described by @BasilBourque:s = s.length() > 10 ? s.substring(0, 10 - 2).concat("..") :...
View Article
More Pages to Explore .....