Java tutorial
//package com.java2s; public class Main { public static String subStringWithByte(String str, int length) { if (str.length() == 0 || length == 0) return ""; StringBuffer buf = new StringBuffer(); for (int i = 0; i < str.length() && length > 0; i++) { char tmp = str.charAt(i); buf.append(tmp); if (tmp > 0 && tmp < 127) { length -= 1; } else { length -= 2; } } return buf.toString(); } }