java中按字节截取String字符串.

作者:网络 来源:佚名 更新时间:2008-02-08 19:42:53 点击:

/**
* 取字符串的前tocount个字符
*
* @param str 被处理字符串
* @param tocount 截取长度
* @param more 后缀字符串
* @version 2004.11.24
* @author zhulx
* @return string
*/
public static string substring(string str, int tocount,string more)
{
int reint = 0;
string restr = "";
if (str == null)
return "";
char[] tempchar = str.tochararray();
for (int kk = 0; (kk < tempchar.length && tocount > reint); kk++) {
string s1 = str.valueof(tempchar[kk]);
byte[] b = s1.getbytes();
reint += b.length;
restr += tempchar[kk];
}
if (tocount == reint || (tocount == reint - 1))
restr += more;
return restr;
}