public class ByteSub { public static void subString(String str, int a) { int bytes = 0; String result = ""; for (int x = 0; x < str.length(); x++) { byte[] b = (str.charAt(x) + "").getBytes(); bytes += b.length; if (bytes > a) { break; } else { result += str.charAt(x); } } System.out.println(result); } public static void main(String[] args) { subString("中国abcd", 3); } }
时间: 2024-11-13 04:56:13