Trim a string based on the string length

后端 未结 12 795
無奈伤痛
無奈伤痛 2020-12-12 14:29

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

相关标签:
12条回答
  • 2020-12-12 15:00

    Here is the Kotlin solution

    One line,

    if (yourString?.length!! >= 10) yourString?.take(90).plus("...") else yourString
    

    Traditional,

    if (yourString?.length!! >= 10) {
      yourString?.take(10).plus("...")
     } else {
      yourString
     }
    
    0 讨论(0)
  • 2020-12-12 15:01

    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);
    
    0 讨论(0)
  • 2020-12-12 15:02

    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 difference between correct code and usual code in this sample:

    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)));
        }
    }
    
    0 讨论(0)
  • 2020-12-12 15:05

    StringUtils.abbreviate from Apache Commons Lang library could be your friend:

    StringUtils.abbreviate("abcdefg", 6) = "abc..."
    StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
    StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
    StringUtils.abbreviate("abcdefg", 4) = "a..."
    

    Commons Lang3 even allow to set a custom String as replacement marker. With this you can for example set a single character ellipsis.

    StringUtils.abbreviate("abcdefg", "\u2026", 6) = "abcde…"
    
    0 讨论(0)
  • 2020-12-12 15:05

    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 
            return input.substring(0, maxLength-2) + "..";
    }
    
    0 讨论(0)
  • 2020-12-12 15:06

    There is a Apache Commons 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 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

    0 讨论(0)
提交回复
热议问题