Java String Truncate truncateString(String source, int prefixLength, int totalLength)

Here you can find the source of truncateString(String source, int prefixLength, int totalLength)

Description

Description: short a string to the format xx...xxxx

License

Open Source License

Parameter

Parameter Description
source the string to shorten
prefixLength number of prefix charts to keep
totalLength desired total length

Declaration

public static String truncateString(String source, int prefixLength, int totalLength) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *                       I N T E L   C O R P O R A T I O N
 * //ww  w.ja v a  2  s  .  co  m
 *  Functional Group: Fabric Viewer Application
 * 
 *  File Name: Util.java
 * 
 *  Archive Source: $Source$
 * 
 *  Archive Log: $Log$
 *  Archive Log: Revision 1.18  2015/08/18 14:28:35  jijunwan
 *  Archive Log: PR 130033 - Fix critical issues found by Klocwork or FindBugs
 *  Archive Log: - DateFormat is not thread safe. Changed to create new DateFormat to avoid sharing it among different threads
 *  Archive Log:
 *  Archive Log: Revision 1.17  2015/08/17 18:54:12  jijunwan
 *  Archive Log: PR 129983 - Need to change file header's copyright text to BSD license txt
 *  Archive Log: - changed frontend files' headers
 *  Archive Log:
 *  Archive Log: Revision 1.16  2015/07/28 18:29:10  fisherma
 *  Archive Log: PR 129219 - Admin page login dialog improvement
 *  Archive Log:
 *  Archive Log: Revision 1.15  2015/07/14 17:00:59  jijunwan
 *  Archive Log: PR 129541 - Should forbid save or deploy when there is invalid edit on management panel
 *  Archive Log: - removed unnecessary argument for warning message display
 *  Archive Log:
 *  Archive Log: Revision 1.14  2015/06/10 19:58:58  jijunwan
 *  Archive Log: PR 129120 - Some old files have no proper file header. They cannot record change logs.
 *  Archive Log: - wrote a tool to check and insert file header
 *  Archive Log: - applied on backend files
 *  Archive Log:
 * 
 *  Overview:
 * 
 *  @author: jijunwan
 * 
 ******************************************************************************/

public class Main {
    /**
     * 
     * <i>Description:</i> short a string to the format xx...xxxx
     * 
     * @param source
     *            the string to shorten
     * @param prefixLength
     *            number of prefix charts to keep
     * @param totalLength
     *            desired total length
     * @return
     */
    public static String truncateString(String source, int prefixLength, int totalLength) {
        if (source.length() <= totalLength) {
            return source;
        }

        if (prefixLength + 3 > totalLength) {
            throw new IllegalArgumentException("total length meust be larger than prefix length + 3!");
        }

        int tokeep = totalLength - prefixLength - 3;
        return source.substring(0, prefixLength) + "..."
                + source.substring(source.length() - tokeep, source.length());
    }
}

Related

  1. truncateString(String description, int length)
  2. truncateString(String errCode, int length, boolean isRight)
  3. truncateString(String inStr, int maxValue)
  4. truncateString(String s)
  5. truncateString(String s, int numChars)
  6. truncateString(String source, String suffix)
  7. truncateString(String str, int length)
  8. truncateString(String str, int length, String suffix)
  9. truncateString(String str, int maxlen)