Java String Truncate truncate(String str, int len)

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

Description

truncate

License

Open Source License

Declaration

public static String truncate(String str, int len) 

Method Source Code

//package com.java2s;
/*/*from  www  .ja  va 2 s.c  o m*/
 * M2M ServiceFOTA ONM version 1.0
 *
 *  Copyright ? 2014 kt corp. All rights reserved.
 *
 *  This is a proprietary software of kt corp, and you may not use this file except in
 *  compliance with license agreement with kt corp. Any redistribution or use of this
 *  software, with or without modification shall be strictly prohibited without prior written
 *  approval of kt corp, and the copyright notice above does not evidence any actual or
 *  intended publication of such software.
 */

public class Main {

    public static String truncate(String str, int len) {
        if (str == null)
            return "";

        String crop = str;
        int slen = 0;
        int blen = 0;
        char c;

        try {
            while (blen + 1 <= len) {
                c = crop.charAt(slen);
                blen++;
                slen++;
                if (c > 127)
                    blen++; //2-byte character..
            }
            crop = crop.substring(0, slen);
        } catch (Exception e) {
            //           logger.error("StringTool.cropByte() :::"+ e.toString());
        }

        return crop;
    }
}

Related

  1. truncate(String source, int maxLength, String cutoffReplacement)
  2. truncate(String src, int maxChars, boolean addEllipses)
  3. truncate(String str)
  4. truncate(String str, int byteLength)
  5. truncate(String str, int chars)
  6. truncate(String str, int length)
  7. truncate(String str, int length)
  8. truncate(String str, int length)
  9. truncate(String str, int length)