private static String getChar(String input, int pos)
{
int count = 0; //声明变量
int i = 0;
for (;i < input.length() && count < pos; i++)
{
char c = input.charAt(i);
if (c > 255) {
if (count + 2 <= pos)
{
count += 2;
} else {
break;
}
}
else
{
count += 1;
}
}
return input.substring(0, i);
}
public static void main(String[] args)
{
sc = new Scanner(System.in);
System.out.println("输入文字");
String line = sc.nextLine();
System.out.println("输入需要截取的字节");
int pos = sc.nextInt();
System.out.println(getChar(line, pos));
} |