public static String jiequ(String s,int len){ String res = ""; char c[] = s.toCharArray(); int i = 0;//循环的次数 int m = 0;//取得字符的位置 while(i < len){ if((int)c[m]<256){ res = res + c[m]; m++; i++; }else{ i = i+2; if(i<len){ res = res + c[m]; m++; }else{ break; } } }
return res; }
public String subString(String str, int subBytes) { int bytes = 0; // 用来存储字符串的总字节数 for (int i = 0; i < str.length(); i++) { if (bytes == subBytes) { return str.substring(0, i); } char c = str.charAt(i); if (c < 256) { bytes += 1; // 英文字符的字节数看作1 } else { bytes += 2; // 中文字符的字节数看作2 if(bytes - subBytes == 1){ return str.substring(0, i); } } } return str; }
public class JieQu { public static void main(String[] args) { System.out.println(jieQu("我ABC你DEF", 6)); } public static String jieQu(String s, int n) { int k; int m = 0; byte a[] = s.getBytes(); for (int i = 0; i < n; i++) { if (a[i] < 0) { m++; } } if (m % 2 == 0) { k = n; } else { k = n -1; } return new String(a, 0, k); }}刚写的,测了,可以~~说实话,不相信这是面试题,太容易了~~ 睡觉去了~~
我也来凑热闹, 哈哈:public class Test { public static void main(String[] args) { String str = "我是Ni你"; String result = ""; int count = 3; int index = 0; for (int i = 0; i < str.length(); ++i) { int first = (str.charAt(i) >> 8) & 0xFF;
if (first > 0) { if (index + 2 <= count) { result += str.charAt(i); index += 2; } else { break; } } else { result += str.charAt(i); ++index; } }
System.out.println(result); } }
下面是我的代码,测试结果的对的; public class ShowString { /** * @param args */ public static void main(String[] args) {
String res = "";
char c[] = s.toCharArray();
int i = 0;//循环的次数
int m = 0;//取得字符的位置
while(i < len){
if((int)c[m]<256){
res = res + c[m];
m++;
i++;
}else{
i = i+2;
if(i<len){
res = res + c[m];
m++;
}else{
break;
}
}
}
return res;
}
int bytes = 0; // 用来存储字符串的总字节数
for (int i = 0; i < str.length(); i++) {
if (bytes == subBytes) {
return str.substring(0, i);
}
char c = str.charAt(i);
if (c < 256) {
bytes += 1; // 英文字符的字节数看作1
} else {
bytes += 2; // 中文字符的字节数看作2
if(bytes - subBytes == 1){
return str.substring(0, i);
}
}
}
return str;
}
System.out.println("a".getBytes().length) ;
System.out.println("有".getBytes().length) ; 可以解决你的问题
System.out.println(jieQu("我ABC你DEF", 6));
} public static String jieQu(String s, int n) {
int k;
int m = 0;
byte a[] = s.getBytes();
for (int i = 0; i < n; i++) {
if (a[i] < 0) {
m++;
}
}
if (m % 2 == 0) {
k = n;
} else {
k = n -1;
}
return new String(a, 0, k);
}}刚写的,测了,可以~~说实话,不相信这是面试题,太容易了~~
睡觉去了~~
public static void main(String[] args) {
String str = "我是Ni你";
String result = "";
int count = 3;
int index = 0;
for (int i = 0; i < str.length(); ++i) {
int first = (str.charAt(i) >> 8) & 0xFF;
if (first > 0) {
if (index + 2 <= count) {
result += str.charAt(i);
index += 2;
} else {
break;
}
} else {
result += str.charAt(i);
++index;
}
}
System.out.println(result);
}
}
public class ShowString { /**
* @param args
*/
public static void main(String[] args) {
String str = "我abc你bcd";
System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));
}//end main
private static String getString(String str, int index) {
int i = 0;
int count = -1;
int c = str.charAt(i);
while (c != '\n') {
if (c > 255) { //表示只有低8为有值
count += 2;
} else { //高8位也有值
count++;
}
if (count >= index) { //当计数器等于下标时,截断
return str.substring(0, i);
}
c = str.charAt(++i);
}
return null;
}//end getString}测试结果:
字符串:我abc你bcd 参数:4
我ab
字符串:我abc你bcd 参数:6
我abc
@author 王平
2010-2-1
*/
public class ShowString { /**
* @param args
*/
public static void main(String[] args) {
String str = "我abc你bcd";
System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));
}//end main
private static String getString(String str, int index) {
int i = 0;
int count = -1;
int c = str.charAt(i);
while (c != '\n') {
if (c > 255) { //表示只有低8为有值
count += 2;
} else { //高8位也有值
count++;
}
if (count >= index) { //当计数器等于下标时,截断
return str.substring(0, i);
}
c = str.charAt(++i);
}
return null;
}//end getString}
/**
@author 王平
2010-2-1
*/
public class ShowString { /**
* @param args
*/
public static void main(String[] args) {
String str = "我abc你bcd";
System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));
}//end main
private static String getString(String str, int index) {
int i = 0;
int count = -1;
int c = str.charAt(i);
while (c != '\n') {
if (c > 255) { //表示只有低8为有值
count += 2;
} else { //高8位也有值
count++;
}
if (count >= index) { //当计数器等于下标时,截断
return str.substring(0, i);
}
c = str.charAt(++i);
}
return null;
}//end getString}
public class ShowString { /**
* @param args
*/
public static void main(String[] args) {
String str = "我abc你bcd";
System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 9));
System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));
}//end main
private static String getString(String str, int index) {
int i = 0;
int count = -1;
int c = str.charAt(i);
while (c != '\0') {
if (c > 255) { //表示只有低8为有值
count += 2;
} else { //高8位也有值
count++;
}
if (count >= index) { //当计数器等于下标时,截断
return str.substring(0, i);
}
c = str.charAt(++i);
}
return null;
}//end getString}
byte[] data = str.getBytes();
byteLength = byteLength > data.length ? data.length : byteLength;
for(int i = 1; ;i++){
if(i == byteLength + 1 || data[byteLength - i] > 0){
if(i % 2 == 0){
byteLength --;
}
break;
}
}
return new String(data, 0, byteLength);
}
byte[] data = str.getBytes();
byteLength = byteLength > data.length ? data.length : byteLength;
for(int i = 1; ;i++){
if(i == byteLength + 1 || data[byteLength - i] > 0){
if(i % 2 == 0){
byteLength --;
}
break;
}
}
return new String(data, 0, byteLength);
}
String s = "我abc你";//在utf-8编码中占9个字节
String res = formatStringInByteLength_utf8(s, 8);
System.out.println(res);
}
public static String formatStringInByteLength_utf8(String str, int byteLength) throws UnsupportedEncodingException{
byte[] data = str.getBytes("UTF-8");
for(byte b : data){
System.out.println(String.format("%x", b));
}
byteLength = byteLength > data.length ? data.length : byteLength;
int count = 0;
while((data[byteLength - 1] & 192) == 128){
byteLength --;
count ++;
}
if(count != 0){
if(count == 1 && (data[byteLength - 1] & 224) == 192
|| count == 2 && (data[byteLength - 1] & 224) == 224){
byteLength += count;
}
else{
byteLength --;
}
}
else if((data[byteLength - 1] & 128) == 128){
byteLength --;
}
return new String(data, 0, byteLength, "UTF-8");
}