如下
public class Test
{
public static void main(String[] args)
{
String str = "我ABC们DEF";
int n = 6;
Test t = new Test();
String temp = t.getSubStr(str, n);
System.out.println(temp);
}
public String getSubStr(String str, int n)
{
if(n >= str.getBytes().length)
{
return str;
}
int i = 0;
int sum = 0;
while(true)
{
if(str.substring(i, i+1).getBytes().length > 1)
{
sum += 2;
}
else
{
sum++;
}
if(sum >= n)
{
if(sum == n)
{
i++;
}
break;
}
i++;
}
return str.substring(0, i);
}
}