java编程:截取字符串

我有酒你把我灌醉 2012-01-13 03:04:46
6、java编写一个截取字符串的函数,输入一个字符串和要截取的字节数,输出按字节数截取的字符串,但是要保证汉字不能截半个,,如“我ABC”,4 应该截取“我AB”,输入“中ABC国DEF人”,6 ,然后输出“中ABC国”,而不是半个“国”字,保留中文(注意:一个汉字占两个字节,此题有多种解法,请首先独立思考和完成,编写自己的程序实现后,再和其他同学的方案进行比较,然后找出最优方案)
...全文
229 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dntg007 2012-01-18
  • 打赏
  • 举报
回复
不看效率的话
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;
menglingwen 2012-01-17
  • 打赏
  • 举报
回复
public class lianxi {
public static void main(String[] args) {
String s="中ABC国DEF人";
String jiequ=s.substring(0,5);
System.out.println(jiequ);
}
}
貌似可以
玉女 2012-01-13
  • 打赏
  • 举报
回复
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
玉女 2012-01-13
  • 打赏
  • 举报
回复
[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]
cwjbeyond 2012-01-13
  • 打赏
  • 举报
回复
1.统一转码为utf-8
2.根据utf-8的编码规则
http://hi.baidu.com/zc030802104/blog/item/b0f84303cfed45074bfb5131.html
如果要截取的最后一个字节的第一位为1:则继续追加后一位字节,直到该字节的第一位为0;
也可以直接追加后一位字节。
andycpp 2012-01-13
  • 打赏
  • 举报
回复
		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();
}
zc499498753 2012-01-13
  • 打赏
  • 举报
回复
上面的有点小误差,这是最终版
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));
}
}
}
zc499498753 2012-01-13
  • 打赏
  • 举报
回复
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));
}
}
酷玩时刻 2012-01-13
  • 打赏
  • 举报
回复
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));//字符串转换成数字型
}

}
miaomiao_j 2012-01-13
  • 打赏
  • 举报
回复
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);
}
dntg007 2012-01-13
  • 打赏
  • 举报
回复
"请首先独立思考和完成"

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧