Here you can find the source of substring(String str, int toCount)
public static String substring(String str, int toCount)
//package com.java2s; //License from project: Apache License public class Main { public static String substring(String str, int toCount) { if (str == null) return ""; int reInt = 0; StringBuilder reStr = new StringBuilder(); char[] tempChar = str.toCharArray(); int len = tempChar.length; for (int kk = 0; (kk < len && toCount > reInt); kk++) { char currentChar = tempChar[kk]; String s1 = String.valueOf(currentChar); byte[] b = s1.getBytes(); int bLen = b.length; if (reInt + bLen > toCount) { break; }/*from w ww .j a va 2s.c o m*/ reInt += bLen; reStr.append(currentChar); } reStr.trimToSize(); return reStr.toString(); } }