Here you can find the source of truncate(String text, int len)
Parameter | Description |
---|---|
text | The text to truncate |
len | The desired length (in codepoints) of the result |
public static String truncate(String text, int len)
//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)); } } }