截取带中文的字符串

Posted on 2008-02-25 11:38 angel 阅读(310) 评论(0)  编辑  收藏
如:abc中国  
package com;

class splitstring {
    
private String str;

    
private int bytenum;

    
public splitstring() {
    }


    
public splitstring(String str, int bytenum) {
        
this.str = str;
        
this.bytenum = bytenum;

    }


    
public void splitit() {

        
byte bt[] = str.getBytes();
        
if (bytenum > 1{
            
            
if(bt[bytenum]<0){
                String str
=new String(bt,0,--bytenum);
                System.out.print(str);
            }
else{
                String str
=new String(bt,0,bytenum);
                System.out.print(str);
            }


        }
 else {
            
if(bytenum==1){
                
if(bt[bytenum]<0){
                    String str
=new String(bt,0,++bytenum);
                    System.out.print(str);
                }
else{
                    String str
=new String(bt,0,bytenum);
                    System.out.print(str);
                }

            }
else{
                System.out.print(
"错误");    
            }

        }

    }

}


public class Test {

    
public static void main(String arg[]) {
        String str 
= "我abcd汉def";
        
int num = 7;
        splitstring sptstr 
= new splitstring(str, num);
        sptstr.splitit();
    }


}

输出为:我abcd

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


网站导航:
 

Copyright © angel