Trim string based on 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?

+75
java string
Dec 14 '11 at 5:03
source share
7 answers
 s = s.substring(0, Math.min(s.length(), 10)); 

Using Math.min like this Math.min exception when the line is already shorter than 10 .




Notes:

  • The above is real cropping. If you really want to replace the last three characters (!) With Dots, if they are truncated, use Apache Commons StringUtils.abbreviate .

  • This may behave incorrectly 1 if your string contains Unicode encodings outside of BMP; e.g. Emojis. For a solution that works correctly for all Unicode code codes, see @sibnick Solution.




1 - Unicode code that is not on plane 0 (BMP) is represented as a "surrogate pair" (i.e. two char values) in a String . Ignoring this, we can reduce to less than 10 code points or (worse) truncate in the middle of a surrogate pair. String.length() , on the other hand, is no longer an ideal measure of text length in Unicode, so cropping based on this might be wrong.

+151
Dec 14 '11 at 5:16
source share

StringUtils.abbreviate from the Apache Commons Lang Library may be your friend:

 StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." 
+77
Nov 14 '13 at 10:24
source share

There is a StringUtils function that does this.

 s = StringUtils.left(s, 10) 

If len characters are not available or String is NULL, the string will be returned without exception. An empty string is returned if len is negative.

StringUtils.left (null,) = null
StringUtils.left (, -ve) = "
StringUtils.left ("", *) = "
StringUtils.left ("abc", 0) = "
StringUtils.left ("abc", 2) = "ab"
StringUtils.left ("abc", 4) = "abc"

StringUtils.Left JavaDocs

Courtesy: Steeve McCauley

+19
Feb 07 '16 at 10:32
source share

s = s.length() > 10 ? s.substring(0, 9) : s;

+9
Dec 14 '11 at 5:31
source share

As usual, no one cares about UTF-16 surrogate pairs. Look at them: What are the most common non-BMP Unicode characters in action? Even authors org.apache.commons / commons-lang3

In this example, you can see the difference between the correct code and regular code:

 public static void main(String[] args) { //string with FACE WITH TEARS OF JOY symbol String s = "abcdafghi\uD83D\uDE02cdefg"; int maxWidth = 10; System.out.println(s); //do not care about UTF-16 surrogate pairs System.out.println(s.substring(0, Math.min(s.length(), maxWidth))); //correctly process UTF-16 surrogate pairs if(s.length()>maxWidth){ int correctedMaxWidth = (Character.isLowSurrogate(s.charAt(maxWidth)))&&maxWidth>0 ? maxWidth-1 : maxWidth; System.out.println(s.substring(0, Math.min(s.length(), correctedMaxWidth))); } } 
+9
Aug 26 '15 at 10:12
source share

Or you can simply use this method if you do not have StringUtils:

 public static String abbreviateString(String input, int maxLength) { if (input.length() <= maxLength) return input; else return input.substring(0, maxLength-2) + ".."; } 
+5
Aug 26 '15 at 9:05
source share

Just in case, you are looking for a way to trim and save LAST 10 characters of a string.

s = s.substring (Math.max (s.length (), 10) - 10);

0
May 26 '17 at 9:20 a.m.
source share



All Articles