Java String Truncate truncate(String text, int len)

Here you can find the source of truncate(String text, int len)

Description

Truncate the supplied text to a maximum of len codepoints.

License

Open Source License

Parameter

Parameter Description
text The text to truncate
len The desired length (in codepoints) of the result

Return

The truncated string

Declaration

public static String truncate(String text, int len) 

Method Source Code

//package com.java2s;
/**************************************************************************
 OmegaT - Computer Assisted Translation (CAT) tool 
  with fuzzy matching, translation memory, keyword search, 
  glossaries, and translation leveraging into updated projects.
    /*from  ww  w  . j  a  v a  2  s.c o  m*/
 Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
       2007 Didier Briel and Tiago Saboga
       2007 Zoltan Bartko - bartkozoltan@bartkozoltan.com
       2008 Andrzej Sawula
       2010-2013 Alex Buloichik
       2015 Zoltan Bartko, Aaron Madlon-Kay
       2016 Aaron Madlon-Kay
       Home page: http://www.omegat.org/
       Support center: http://groups.yahoo.com/group/OmegaT/
    
 This file is part of OmegaT.
    
 OmegaT is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 OmegaT is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/

public class Main {
    public static final char TRUNCATE_CHAR = '\u2026';

    /**
     * Truncate the supplied text to a maximum of len codepoints. If truncated,
     * the result will be the first (len - 1) codepoints plus a trailing
     * ellipsis.
     * 
     * @param text
     *            The text to truncate
     * @param len
     *            The desired length (in codepoints) of the result
     * @return The truncated string
     */
    public static String truncate(String text, int len) {
        if (text.codePointCount(0, text.length()) <= len) {
            return text;
        }
        return firstN(text, len - 1) + TRUNCATE_CHAR;
    }

    /**
     * Extracts first N codepoints from string.
     */
    public static String firstN(String str, int len) {
        if (str.codePointCount(0, str.length()) <= len) {
            return str;
        } else {
            return str.substring(0, str.offsetByCodePoints(0, len));
        }
    }
}

Related

  1. truncate(String string, int maxLength, boolean atBeginning, String ellipse)
  2. truncate(String string, int maxSize)
  3. truncate(String string, int n, String suffix)
  4. truncate(String strOrginal, int iByteMaxSize)
  5. truncate(String text)
  6. truncate(String text, int length)
  7. truncate(String text, int length)
  8. truncate(String text, int maxLength)
  9. truncate(String text, int size, String charsetName)