import java.util.Scanner;
/* 指定截取字节数,截取字符串,但是不能出现半个汉字的情况,例如:
字符串"喊sd中d王",长度2byte为:喊,长度3byte为:喊s,长度4byte为:喊sd,
长度5byte为:喊sd,长度6byte为:喊sd中
*/
public class SplitTest{
public static void main(String[] args){
String str="喊sd中d王";
int max=0;
System.out.println("请输入要截取的字符数:");
//当输入为0时,结束循环
while((max=new Scanner(System.in).nextInt())!=0){
System.out.println("请输入要截取的字符数:");
System.out.print("截取后的结果为:");
System.out.println(splitStr(str,max));
}
/*int start=0;
int max=0;
while(true){
System.out.println("请输入起始位置字节(从0开始):");
start=new Scanner(System.in).nextInt();
System.out.println("请输入要截取的字符数:");
max=new Scanner(System.in).nextInt();
System.out.print("截取后的结果为:");
System.out.println(splitStr(str,start,max));
}*/
}
//从0开始截取长度为byteCount字节的字符串
public static String splitStr(String s, int byteCount){
if(s==null||byteCount<1){
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载