public void cutString(String str,int n)
{
String temp;
int count=0;
for (int i = 0; i < str.length(); i++) {
temp=str.charAt(i)+"";
count+=temp.getBytes().length;
if(count>n)
break;
System.out.print(temp);
}
}
@Test
public void testString()
{
cutString("我ABC好的sfd", 7);
}
{
String temp;
int count=0;
for (int i = 0; i < str.length(); i++) {
temp=str.charAt(i)+"";
count+=temp.getBytes().length;
if(count>n)
break;
System.out.print(temp);
}
}
@Test
public void testString()
{
cutString("我ABC好的sfd", 7);
}
本文介绍了一个简单的Java方法,用于按字节长度截断包含中文和英文字符的字符串,并提供了一个测试示例。
559

被折叠的 条评论
为什么被折叠?



