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 閱讀(122) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 台湾省| 沈丘县| 天气| 肇庆市| 旌德县| 珠海市| 彰武县| 竹山县| 浑源县| 偃师市| 六枝特区| 南宫市| 新绛县| 泰来县| 桓台县| 灌南县| 南投县| 鄢陵县| 横峰县| 额尔古纳市| 汤原县| 山西省| 翼城县| 望谟县| 汪清县| 隆林| 大埔县| 龙海市| 临洮县| 梅河口市| 安多县| 永清县| 资阳市| 太原市| 隆昌县| 陕西省| 文安县| 临漳县| 常山县| 仙游县| 永定县|