java编程:截取字符串
6、java编写一个截取字符串的函数,输入一个字符串和要截取的字节数,输出按字节数截取的字符串,但是要保证汉字不能截半个,,如“我ABC”,4 应该截取“我AB”,输入“中ABC国DEF人”,6 ,然后输出“中ABC国”,而不是半个“国”字,保留中文(注意:一个汉字占两个字节,此题有多种解法,请首先独立思考和完成,编写自己的程序实现后,再和其他同学的方案进行比较,然后找出最优方案)
[解决办法]
public static void main(String[] args) {
int j = 9; //为输入的需要截取的字节个数
String str = "我abcfd是a中国人";
byte[] bt = str.getBytes();
if(bt[j]<0&&bt[j-1]<0){
j++;
}
byte[] b = new byte[j];
for(int i=0;i<j;i++){
b[i]=bt[i];
}
String ss = new String(b);
System.out.println(ss);
}
[解决办法]
package com.zuo.test;
public class Test {
public static void main(String[] args){
String str1="Mstanford";
String str2="123";
System.out.println(str1.length());//长度
System.out.println(str1.compareTo(str2));//比较字符串的大小
System.out.println(str1.concat(str2));//拼接
System.out.println(str1.equals(str2));//判断是否相等
System.out.println(str1.substring(2,5));//截取
System.out.println(String.valueOf(str2));//字符串转换成数字型
}
}
[解决办法]
public static void main(String[] args) {
String str = "中ABC国DEF人";
exec(str,6);
}
public static void exec(String str,int N){
ArrayList<String> result = new ArrayList<String>();
int i = 0; // 字符串定位
int left = N;
int count = 0; // 当前已划分出来的块数
while (i < str.length()) {
if (left - (String.valueOf(str.charAt(i))).getBytes().length >= 0) {
if (result.size() == 0) {
result.add(String.valueOf(str.charAt(i)));
} else {
result.set(count, ((String) result.get(count))+(str.charAt(i)));
}
left -= (String.valueOf(str.charAt(i))).getBytes().length;
i++;
} else {
count++;
result.add("");
left = N;
}
}
for (int a = 0; a < result.size(); a++) {
System.out.println(result.get(a));
}
}
[解决办法]
上面的有点小误差,这是最终版
public static void main(String[] args) {
String str = "中ABC国DEF人";
exec(str,6);
}
public static void exec(String str,int N){
ArrayList<String> result = new ArrayList<String>();
int i = 0; // 字符串定位
int left = N;
int count = 0; // 当前已划分出来的块数
while (i < str.length()) {
if (left - (String.valueOf(str.charAt(i))).getBytes().length >= -1) {
if (result.size() == 0) {
result.add(String.valueOf(str.charAt(i)));
} else {
result.set(count, ((String) result.get(count))+(str.charAt(i)));
}
left -= (String.valueOf(str.charAt(i))).getBytes().length;
i++;
} else {
count++;
result.add("");
left = N;
}
}
for (int a = 0; a < result.size(); a++) {
System.out.println(result.get(a));
}
}
}
------解决方案--------------------
String s = "中ABC国DEF人"; byte[] newBytes = null; try { byte[] bs = s.getBytes("GBK"); boolean flag = false; int i; for(i=0; i<6; i++) { //这里的6是你要截取的字节数 if(flag==true) { flag = false; }else if((bs[i] & 0xff)>=0x81) { flag = true; } } if(flag == true) { i++; } newBytes = Arrays.copyOf(bs, i); s = new String(newBytes, "GBK"); System.out.println(s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
[解决办法]
1.统一转码为utf-8
2.根据utf-8的编码规则
http://hi.baidu.com/zc030802104/blog/item/b0f84303cfed45074bfb5131.html
如果要截取的最后一个字节的第一位为1:则继续追加后一位字节,直到该字节的第一位为0;
也可以直接追加后一位字节。
[解决办法]
[Java code]public static void main(String[] args) {
String a="我A是B中C国D人E";
String b=a.substring(0,3);
System.out.println(b);
}[Java code]
[解决办法]
public static void main(String[] args) {
String a="我A是B中C国D人E";
String b=a.substring(0,3);
System.out.println(b);
}
输出结果 我A是
Text file encoding UTF-8 或 GBK
[解决办法]
public class lianxi {
public static void main(String[] args) {
String s="中ABC国DEF人";
String jiequ=s.substring(0,5);
System.out.println(jiequ);
}
}
貌似可以
[解决办法]
不看效率的话
int maxSize = 4;
String str = "..........";
for (int i = 0; i < str.length; i++) {
if (str.substring(0, i).getBytes().length > maxSize) {
return str.substring(0, i - 1);
}
}
return str;