Prodigy/kf智
我的博客
posts - 20,comments - 7,trackbacks - 0
import java.util.HashMap;
public class StringTest
{
 public void test(String s,int b)
 {
  char c[]=s.toCharArray();
  HashMap length=new HashMap();
  for(int i=0;i<c.length;i++)
  {
   int temp=Character.getType(c[i])==5 ? 2 : 1;
   length.put(new Integer(i),new Integer(temp));
   //得到每个字符的字节数   
  }
  int temp=0;
  StringBuffer sb=new StringBuffer();
  for(int i=0;i<b;i++)
  {
   int getint=((Integer)length.get(new Integer(i))).intValue();
   if(temp+getint==b)
   {   
    sb.append(c[i]);
    break;
   }
   if(temp+getint<b)
   {
    temp=temp+getint;
    sb.append(c[i]);
   }
   if(temp+getint>b)   
   break;
   }
   System.out.println(sb.toString());
  }      
 public static void main(String args[])
 {
  new StringTest().test("我ABC汉DEF",6);
 }
}
posted on 2006-03-29 11:11 xiaozhi 阅读(113) 评论(0)  编辑  收藏

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


网站导航: