截取制定长度的字符串(经典思路)

2013-08-16

public String getNeedString(String source, int requestLength)
{
    int index = 0;  //begin num
    int sumLength = 0;  //sum length
    String result = "";
    while(index < source.length)
    {
         char currentChar = source.charAt(index);
         int currentCharLenth = ("" + currentChar).getBytes().length();
         sumlength += currentCharLength;
         if(sumlength>requestLength)
          {
              break;
           }
          result += currentChar;
          index++;
     }
    
}

 

posted @ 2013-08-16 22:25  Brook Legend  阅读(205)  评论(0编辑  收藏  举报