几道java面试题讨论 问题2,编写一个字符串截取程序,要求按字节长度截取一个字节数组形势的字符串,字符包含中英文,要求最后截取的是半个中文字符,舍弃它,看大家有没有好的算法。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 写一个方法 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 ""; }} String str = "我ABC汉DEF"; int isize = 7; int endIndex = isize / 2; String sub = str.substring(0, endIndex); while (sub.getBytes().length < isize) { if (isize - sub.getBytes().length >= 2) { endIndex = endIndex + (isize - sub.getBytes().length) / 2; } else { endIndex++; } String temp = str.substring(0, endIndex); if (temp.getBytes().length - 1 == isize) { break; } else { sub = temp; } } System.out.println(sub);随便写一段玩玩 public static String leftStr(String source, int maxByteLen) { char[] chars = new char[source.length()]; StringBuilder strBuilder = new StringBuilder(); int copyMaxByteLen = maxByteLen; source.getChars(0, source.length(), chars, 0); for (char current : chars) { if ((current >>> 8) == 0) { // 只占一个字节。 if (copyMaxByteLen > 0) { strBuilder.append(current); copyMaxByteLen--; } else { break; } } else { // 占二个字节。 if (copyMaxByteLen > 1) { strBuilder.append(current); copyMaxByteLen -= 2; } else { break; } } } return strBuilder.toString(); } public static void split(String source,int num) { int k=0; String temp=""; for (int i = 0; i <source.length(); i++) { byte[] b=(source.charAt(i)+"").getBytes(); k+=b.length; if(k>num) { break; } temp+=source.charAt(i); } System.out.println(temp); } public class a1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str="中国A我"; byte[] b="s".getBytes(); new a1().A(str,4); int i=f(13); System.out.println(i); } public void A(String str,int i) { byte b[] = new byte[1024]; int num = 0; b = str.getBytes(); if(b[i-1]>0) { System.out.println(new String(b,0,i)); }else { for(int j=0;j<i;j++) { if(b[j]<0) { num++; num = num%2; }else { num = 0; } } if(num==0) { System.out.println(new String(b,0,i)); }else { System.out.println(new String(b,0,i-1)); } } }} public void A(String str,int i) { byte b[] = new byte[1024]; int num = 0; b = str.getBytes(); if(b[i-1]>0) { System.out.println(new String(b,0,i)); }else { for(int j=0;j<i;j++) { if(b[j]<0) { num++; num = num%2; }else { num = 0; } } if(num==0) { System.out.println(new String(b,0,i)); }else { System.out.println(new String(b,0,i-1)); } } }} 不好意思,上面那个代码粘贴错了,这个是正确的。 public class a1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str="中国A我"; byte[] b="s".getBytes(); new a1().A(str,4); } public void A(String str,int i) { byte b[] = new byte[1024]; int num = 0; b = str.getBytes(); if(b[i-1]>0) { System.out.println(new String(b,0,i)); }else { for(int j=0;j<i;j++) { if(b[j]<0) { num++; num = num%2; }else { num = 0; } } if(num==0) { System.out.println(new String(b,0,i)); }else { System.out.println(new String(b,0,i-1)); } } }} public static String subWord(int n,String s){//截取字符串,不能破汉字 if(s==null || "".trim().equals(null)) return null; int zn=0; for(int i=0;i<s.length();i++){ String c = s.substring(i,i+1); if(c.getBytes().length==2){ zn=zn+2; }else{ zn=zn+1; } if(n<zn){ break; } System.out.println(c); } return null; } 要看字符集的,按楼主意思应该是“GB2312”而不是“UTF-8” 学习,java是很强大的,我们大家要好好的学习哦 java是很强大的,我们大家要好好的学习哦 刚学JAVA一周多点儿,来贵论坛看看。我是菜的不能再菜的选手,想拜师! public static String myCuter(String input,int length){ int inputStrLength=input.length(); byte [] bs=input.getBytes(); int byteLen=bs.length; if(byteLen<length){ System.out.println("out of range!"); return null; } int strOffset=0; int byteOffset=0; StringBuffer sb=new StringBuffer(); while(strOffset<inputStrLength){ String temp=input.substring(strOffset,strOffset+1); byteOffset+=temp.getBytes().length; if(byteOffset>length) break; else{ sb.append(temp); strOffset++; } } return sb.toString(); } 你想多了,考虑java虚拟机就可以了 public String getSubStrByByte(String str, int index) { //假设都是汉字 int endIndex = index / 2; String subStr = str.substring(0, endIndex); //如果小证明不都是汉字 while (subStr.getBytes().length < index) { //累进 endIndex++; String temp = str.substring(0, endIndex); //如果成立证明是汉字,舍弃 if (temp.getBytes().length - 1 == index) { break; } else { subStr = temp; } } return subStr; } public static void main(String[] args) { String str = "中AB国人CD民"; String subStr=new GetSubStrByByte().getSubStrByByte(str, 5); System.out.println("======="+subStr); } 这个题目的关键点在于证明截取的字符是汉字然后舍去汉字的特点是双字符。半个汉字的byte值是负数 写一个方法 String left(String str ,int n) <br> * str字符串中可能包含中文,中文是2bytes,实现的功能是<br> * 如:“中abc12” n=4 则该方法返回“中ab”<br> * “中abc国a” n=6 则返回“中abc”<br> * 中文是一半时不返回 直接在java环境下写字测试当然没问题,如果是从一个文件里读取的,是UTF-16格式的,或别的格式的就不行了 public class testSubString { public static void main(String args[]){ String str = "中国人aaaa中国人很好a"; System.out.print(getSubString(str)); } private static String getSubString(String str) { // TODO Auto-generated method stub String subString=""; int a = 0; //统计汉字的字数数量 int b = 0; //统计字母的数量 byte[] subByte = str.getBytes(); for(int i=0;i<str.getBytes().length;i++){ if(subByte[i]<0){ a++; } else{ b++; } if(subByte[i]>0&&subByte[i+1]<0){ //判断题目要求出现的位置 subString = str.substring(0, a/2+b); //确定输出字符串最后的位置 System.out.println(a); System.out.println(b); break; } } return subString; }}菜鸟第一次回帖,呵呵,试试~以后多来逛逛~ public class testSubString { public static void main(String args[]){ String str = "中国人aaaa中国人很好a"; System.out.print(getSubString(str)); } private static String getSubString(String str) { // TODO Auto-generated method stub String subString=""; int a = 0; //统计汉字的字数数量 int b = 0; //统计字母的数量 byte[] subByte = str.getBytes(); for(int i=0;i<str.getBytes().length;i++){ if(subByte[i]<0){ a++; } else{ b++; } if(subByte[i]>0&&subByte[i+1]<0){ //判断题目要求出现的位置 subString = str.substring(0, a/2+b); //确定输出字符串最后的位置 System.out.println(a); System.out.println(b); break; } } return subString; }}菜鸟第一次回帖,呵呵,试试~以后多来逛逛~ public static void main(String[] args) { String s = "你是a住啊dsw"; System.out.priny(s.substring(0, get(s,5))); } static int get(byte a[], int len) { int count = 0; int index = 0; for (int i = 0; i < len; i++) {//一个中文占2个字节,半个中文的a[i]小于0.连续2个小于表示一个中文 if (a[i] < 0) { count++; if (count == 2) { index++; count = 0; } } else { index++; count = 0; } } return index; } 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 ""; }} byte[] subStr(String str, int len){ if(len <= 0) return new byte[0]; char[] ch = str.toCharArray(); int flag = len; for(int i = 0; i < flag; i++){ if(char[i]>128) flag--; } return str.subString(0,flag+1).getBytes();} 打印图形 关于JAVASE的问题! HSQLDB - 100% Java Database 关于FTP下载编程问题 谁能帮忙解释下面的代码(运行异常) 这句话什么意思 showMessageDialog的使用问题 真郁闷,散分 JBuilder快捷键大搜集,来者有分 这个结果如何理解?(菜鸟级问题) JTable中添加了Jcombobox,如何实现按tab键到该单元格时弹出列表 java环境中需要的安装软件中各个单词的含义?
* 写一个方法 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 "";
}
}
String str = "我ABC汉DEF";
int isize = 7;
int endIndex = isize / 2;
String sub = str.substring(0, endIndex);
while (sub.getBytes().length < isize) {
if (isize - sub.getBytes().length >= 2) {
endIndex = endIndex + (isize - sub.getBytes().length) / 2;
} else {
endIndex++;
}
String temp = str.substring(0, endIndex);
if (temp.getBytes().length - 1 == isize) {
break;
} else {
sub = temp;
}
}
System.out.println(sub);随便写一段玩玩
public static String leftStr(String source, int maxByteLen) {
char[] chars = new char[source.length()];
StringBuilder strBuilder = new StringBuilder();
int copyMaxByteLen = maxByteLen;
source.getChars(0, source.length(), chars, 0);
for (char current : chars) {
if ((current >>> 8) == 0) {
// 只占一个字节。
if (copyMaxByteLen > 0) {
strBuilder.append(current);
copyMaxByteLen--;
} else {
break;
} } else {
// 占二个字节。
if (copyMaxByteLen > 1) {
strBuilder.append(current);
copyMaxByteLen -= 2;
} else {
break;
} } } return strBuilder.toString();
}
{
int k=0;
String temp="";
for (int i = 0; i <source.length(); i++)
{
byte[] b=(source.charAt(i)+"").getBytes();
k+=b.length;
if(k>num)
{
break;
}
temp+=source.charAt(i);
}
System.out.println(temp);
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="中国A我";
byte[] b="s".getBytes();
new a1().A(str,4);
int i=f(13);
System.out.println(i);
}
public void A(String str,int i) {
byte b[] = new byte[1024];
int num = 0;
b = str.getBytes();
if(b[i-1]>0) {
System.out.println(new String(b,0,i));
}else {
for(int j=0;j<i;j++) {
if(b[j]<0) {
num++;
num = num%2;
}else {
num = 0;
}
}
if(num==0) {
System.out.println(new String(b,0,i));
}else {
System.out.println(new String(b,0,i-1));
}
}
}
}
public void A(String str,int i) {
byte b[] = new byte[1024];
int num = 0;
b = str.getBytes();
if(b[i-1]>0) {
System.out.println(new String(b,0,i));
}else {
for(int j=0;j<i;j++) {
if(b[j]<0) {
num++;
num = num%2;
}else {
num = 0;
}
}
if(num==0) {
System.out.println(new String(b,0,i));
}else {
System.out.println(new String(b,0,i-1));
}
}
}
}
public class a1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="中国A我";
byte[] b="s".getBytes();
new a1().A(str,4);
}
public void A(String str,int i) {
byte b[] = new byte[1024];
int num = 0;
b = str.getBytes();
if(b[i-1]>0) {
System.out.println(new String(b,0,i));
}else {
for(int j=0;j<i;j++) {
if(b[j]<0) {
num++;
num = num%2;
}else {
num = 0;
}
}
if(num==0) {
System.out.println(new String(b,0,i));
}else {
System.out.println(new String(b,0,i-1));
}
}
}
}
if(s==null || "".trim().equals(null))
return null;
int zn=0;
for(int i=0;i<s.length();i++){
String c = s.substring(i,i+1);
if(c.getBytes().length==2){
zn=zn+2;
}else{
zn=zn+1;
}
if(n<zn){
break;
}
System.out.println(c);
}
return null;
}
public static String myCuter(String input,int length){
int inputStrLength=input.length();
byte [] bs=input.getBytes();
int byteLen=bs.length;
if(byteLen<length){
System.out.println("out of range!");
return null;
}
int strOffset=0;
int byteOffset=0;
StringBuffer sb=new StringBuffer();
while(strOffset<inputStrLength){
String temp=input.substring(strOffset,strOffset+1);
byteOffset+=temp.getBytes().length;
if(byteOffset>length)
break;
else{
sb.append(temp);
strOffset++;
}
}
return sb.toString();
}
你想多了,考虑java虚拟机就可以了
public String getSubStrByByte(String str, int index) {
//假设都是汉字
int endIndex = index / 2;
String subStr = str.substring(0, endIndex);
//如果小证明不都是汉字
while (subStr.getBytes().length < index) {
//累进
endIndex++;
String temp = str.substring(0, endIndex);
//如果成立证明是汉字,舍弃
if (temp.getBytes().length - 1 == index) {
break;
} else {
subStr = temp;
}
}
return subStr;
}
public static void main(String[] args) {
String str = "中AB国人CD民";
String subStr=new GetSubStrByByte().getSubStrByByte(str, 5);
System.out.println("======="+subStr);
}
汉字的特点是双字符。半个汉字的byte值是负数
* str字符串中可能包含中文,中文是2bytes,实现的功能是<br>
* 如:“中abc12” n=4 则该方法返回“中ab”<br>
* “中abc国a” n=6 则返回“中abc”<br>
* 中文是一半时不返回
public static void main(String args[]){
String str = "中国人aaaa中国人很好a";
System.out.print(getSubString(str));
} private static String getSubString(String str) {
// TODO Auto-generated method stub
String subString="";
int a = 0; //统计汉字的字数数量
int b = 0; //统计字母的数量
byte[] subByte = str.getBytes();
for(int i=0;i<str.getBytes().length;i++){
if(subByte[i]<0){
a++;
}
else{
b++;
}
if(subByte[i]>0&&subByte[i+1]<0){ //判断题目要求出现的位置
subString = str.substring(0, a/2+b); //确定输出字符串最后的位置
System.out.println(a);
System.out.println(b);
break;
}
}
return subString;
}}
菜鸟第一次回帖,呵呵,试试~以后多来逛逛~
public static void main(String args[]){
String str = "中国人aaaa中国人很好a";
System.out.print(getSubString(str));
} private static String getSubString(String str) {
// TODO Auto-generated method stub
String subString="";
int a = 0; //统计汉字的字数数量
int b = 0; //统计字母的数量
byte[] subByte = str.getBytes();
for(int i=0;i<str.getBytes().length;i++){
if(subByte[i]<0){
a++;
}
else{
b++;
}
if(subByte[i]>0&&subByte[i+1]<0){ //判断题目要求出现的位置
subString = str.substring(0, a/2+b); //确定输出字符串最后的位置
System.out.println(a);
System.out.println(b);
break;
}
}
return subString;
}}
菜鸟第一次回帖,呵呵,试试~以后多来逛逛~
String s = "你是a住啊dsw";
System.out.priny(s.substring(0, get(s,5)));
} static int get(byte a[], int len) {
int count = 0;
int index = 0;
for (int i = 0; i < len; i++) {
//一个中文占2个字节,半个中文的a[i]小于0.连续2个小于表示一个中文
if (a[i] < 0) {
count++;
if (count == 2) {
index++;
count = 0;
}
} else {
index++;
count = 0;
}
}
return index;
}
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 "";
}
}
byte[] subStr(String str, int len){
if(len <= 0)
return new byte[0];
char[] ch = str.toCharArray();
int flag = len;
for(int i = 0; i < flag; i++){
if(char[i]>128)
flag--;
}
return str.subString(0,flag+1).getBytes();
}