汉字是双字节,大于255
import java.util.*; public class Main { public Main() { } public String substring(String str, int k) { StringBuilder res = new StringBuilder(); for (char ch : str.toCharArray()) { if (k > 0 && ch < 256) { res.append(ch); k--; } else if (k > 1) { res.append(ch); k -= 2; } else { break; } } return res.toString(); } public static void main(String[] args) { Main solution = new Main(); Scanner in = new Scanner(System.in); while (in.hasNext()) { String str = in.next(); int k = Integer.parseInt(in.next()); String res = solution.substring(str, k); System.out.println(res); } } }