Here you can find the source of subStrByBytes(String str, int len, String tail)
public static String subStrByBytes(String str, int len, String tail)
//package com.java2s; //License from project: Apache License public class Main { public static String subStrByBytes(String str, int len, String tail) { if (str == null) { return ""; }//w w w. j a v a 2s .c om if (str.getBytes().length <= len) { return str.trim(); } str = str.trim(); String s = ""; char[] c = str.toCharArray(); int i = 0; if (tail != null) { len -= tail.getBytes().length; } while (s.getBytes().length < len) { s = s + String.valueOf(c[i]); i++; } if (s.getBytes().length > len) { s = s.substring(0, s.length() - 1); } if (tail != null) s = s + tail; return s; } }