您的位置:首页 > 其它

微软与卡西欧签订多年期专利交叉授权协议

2011-09-21 13:50 633 查看
/**
* 写一个方法 String left(String str ,int n) <br>
* str字符串中可能包含中文,中文是2bytes,实现的功能是<br>
* 如:“中abc12” n=4 则该方法返回“中ab”<br>
* “中abc国a” n=6 则返回“中abc”<br>
* 中文是一半时不返回
*
* @author Fee Share
*/
public class Test {
public static void main(String[] args) {
String s = "我是acf中d国人adfgdd123";
for (int i = 0; i <= s.getBytes().length; i++) {
System.out.printf("%2d=%s\n", i, getSubString(s, i));
}
}

private static String getSubString(String s, int n) {
int count = 0;
int offset = 0;
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] > 256) {
offset = 2;
count += 2;
} else {
offset = 1;
count++;
}
if (count == n) {
return s.substring(0, i + 1);
}
if ((count == n + 1 && offset == 2)) {
return s.substring(0, i);
}
}
return "";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: