public class Demo {
public static void main(String[] args) {
String s = Cut("我ABc",4);//调用切割字符串方法
System.out.println(s);
}
public static String Cut(String str,int len)
{
byte[] arr = str.getBytes();
int count = 0;//定义计数器记录负数的个数
for(int x = len-1; x>=0; x--)
{
if(arr[x]<0) //从后往前看是否有负数若有计数器+1,否则跳出循环;
{
count++;
}
else
break;
}
if(count%2==0)
return new String(arr,0,len);
else
return new String(arr,0,len-1);
}
}
|