package com.accp;public class Test { public static void main(String[] args) {
if(args.length<2){
System.out.println("Usage java Test arg1 arg2");
System.exit(0);
}
int len=Integer.parseInt(args[1]);
String str=args[0];
byte[] buf;
buf=str.getBytes();
byte[] tmp=new byte[len];
for (int i = 0; i < len; i++) {
tmp[i]=buf[i];
}
String result=new String(tmp);
System.out.println(result);
}}我是这样写的,但是还有另外一个要求:输入"我ABC爱ABC" 6 应该输出"我ABC",而不是"我ABC +爱 的一半.这个问题不知道怎么办了..
见:
http://topic.csdn.net/u/20090214/22/3a91cfed-950d-4cf1-b569-1c7348dd0487.html
public static void main(String[] args) {
String s="zg中国zg";
byte[] bytes=s.getBytes();
Scanner sc=new Scanner(System.in);
System.out.println("input number:");
int n=sc.nextInt();
if(n>=bytes.length){
n=bytes.length;
}
for(int i=0;i<n;i++){
String s1=new String(bytes,i,1);
if(!(s1.toCharArray()[0]>='A'&&s1.toCharArray()[0]<='z')){
if(i+1>=n){
break;
}else{
s1=new String(bytes,i,2);i++;
}
}
System.out.print(s1);
}
}
}