/**
* 按指定的字节数截取字符串(一个中文字符占3个字节,一个英文字符或数字占1个字节)
* @param sourceString 源字符串
* @param cutBytes 要截取的字节数
* @return
*/
public static String cutString(String sourceString, int cutBytes)
{
if(sourceString == null || "".equals(sourceString.trim()))
{
return "";
}
int lastIndex = 0;
boolean stopFlag = false;
int totalBytes = 0;
for(int i=0; i<sourceString.length(); i++)
{
String s = Integer.toBinaryString(sourceString.charAt(i));
if(s.length() > 8)
{
totalBytes += 3;
}
else
{
totalBytes += 1;
}
if(!stopFlag)
{
if(totalBytes == cutBytes)
{
lastIndex = i;
stopFlag = true;
}
else if(totalBytes > cutBytes)
{
lastIndex = i - 1;
stopFlag = true;
}
}
}
if(!stopFlag)
{
return sourceString;
}
else
{
return sourceString.substring(0, lastIndex + 1);
}
}
JAVA按指定的字节数截取字符串
最新推荐文章于 2024-07-30 03:18:59 发布