b47617

攀登!进步!
随笔 - 20, 文章 - 1, 评论 - 0, 引用 - 0
数据加载中……

一道面试题

编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如ABC”4,应该截为AB”,输入ABCDEF”6,应该输出为ABC”而不是ABC+汉的半个
代码如下:
public class SplitString{
            String splitString;
             int splitByte;
            public SplitString(String str,int n){
                  splitString = str;
                  splitByte = n;
                  System.out.println("This String is:" + splitString + "splitbyte" + splitByte);
            }
         public void split(){
                  int loopnum;
                  loopnum = (splitString.length()%splitByte == 0)?(splitString.length()/splitByte):(splitString.length()/splitByte + 1);
                  for(int i = 1;i <= loopnum; i++){
                        if(i == loopnum)
                                System.out.println(splitString.substring((i-1)*splitByte,splitString.length()));
                       else
                                 System.out.println(splitString.substring((i-1)*splitByte,i*splitByte));
                  }
         }
         public static void main(String args[]){
            SplitString spstr = new SplitString("这是my javablog,欢迎大家来访",4);
            spstr.split();
         }
}

posted on 2006-03-11 13:49 原语 阅读(106) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: