/*
Author:lxrmido
Website:buzhuanggeek.com
Edit:GVIM_073
Date:2011/09/10
*/
import javax.swing.*;
class hw_7{
public static void main(String[] args){
String str = JOptionPane.showInputDialog(null, "请输入待截取的字符串:");
String bytess = JOptionPane.showInputDialog(null, "请输入要截取的字节数:");
while(!(bytess.matches("\\d+"))){
bytess = JOptionPane.showInputDialog(null, "输入有误,请重新输入要截取的字节数:");
}
int bytes = Integer.parseInt(bytess);
int index = 0;
int endindex = 0;
char strc[] = str.toCharArray();
for(int i:strc){
if(i > 256){
if((index == bytes) || (index == bytes-1)){
break;
}
index += 2;
endindex ++;
}else{
if(index == bytes){
break;
}
index ++;
endindex ++;
}
}
JOptionPane.showMessageDialog(null, "截取的字符串为:"+ str.substring(0, endindex));
System.exit(0);
}
}