字符串的截取,

yunshangcao26 2009-08-24 04:11:06
我现在要将一系列的中文和英文的标题展示到页面的标题列表中,要求中英文标题的长度大致相等,并且英文不允许从一个单词中间截开,
比如:如果标题为如下两个:
(1)如果一个人轻轻走过,你是否被他的清风带动
(2)if a man came by with you lightly, whether you would be reflected by him
如果我指定长度为10,则第一个标题为:
如果一个人轻轻走过...
第二个为:
if a man ...(由于c在come中前半部分位置,故不要整个单词)
此时两个标题的长度的差别就很明显,那么我如何能将两个标题的长度条的差不多呢,当然可以修改第二个的长度,但是必须得以给定的长度为标准。接口方法 SubStr(String title,int lengthAssigned);

在线等,急用,
...全文
355 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
yunshangcao26 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 sunnyfun888 的回复:]
引用 26 楼 yunshangcao26 的回复:
引用 23 楼 sunnyfun888 的回复:
引用 20 楼 yunshangcao26 的回复:

String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";
 


越来越复杂了,还带转义字符,呵呵。


呵呵,标题是任意的



如果有转义字符的话,若正好截到半个转义字符,那显示出来就会给人乱码的感觉。
还需要对这加以控制...
[/Quote]
转义字符不用解析吧,
sunnyfun888 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 yunshangcao26 的回复:]
引用 23 楼 sunnyfun888 的回复:
引用 20 楼 yunshangcao26 的回复:

String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";
 


越来越复杂了,还带转义字符,呵呵。



呵呵,标题是任意的

[/Quote]

如果有转义字符的话,若正好截到半个转义字符,那显示出来就会给人乱码的感觉。
还需要对这加以控制...
yunshangcao26 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 rascalboy520 的回复:]
Java codepublicstatic String subString(String str,int sub) {if (str!=null) {
Pattern pattern= Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]", Pattern.CASE_INSENSITIVE| Pattern.DOTALL);
Matcher m= pattern.matcher(str);if (m.find()) {//中文if (str.length()<= sub) {return str;
}
System.out.print("中文:");
str= str.substring(0, sub)+"...";return str;
}else {
System.out.print("英文:");
sub*=2;if (str.length()<= sub) {return str;
}
String str1= str.substring(0, sub);
String str2= str.substring(sub, str.length());int a= str1.substring(str1.lastIndexOf(""), str1.length()).length();int b= str2.substring(0, str2.indexOf("")).length();
str= a>= b? str.substring(0, sub+ b) : str.substring(0, sub- a);return str+"...";
}
}returnnull;
}publicstaticvoid main(String[] args) {
String str="如果一个人轻轻走过,你是否被他的清风带动";
System.out.println(subString(str,10));
str="if a man came by with you lightly, whether you would be reflected by him";
System.out.println(subString(str,10));
}
这个是加了缩进的
[/Quote]
哥们 ,你的程序只能进入到中文:
这是我的测试代码:
public static void main(String[] args) {
String str = "UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换";
String str1 = "Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle; ";

// System.out.println(subString(str, 10));
// System.out.println(subString(str1, 10));
for(int i=0;i<50;i++){
System.out.println(subString(str, i));
System.out.println(subString(str1, i));
}
yunshangcao26 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 yunshangcao26 的回复:]
引用 33 楼 lcj_up 的回复:
刚刚自己做的,应该可以!!

import java.util.StringTokenizer;
public class equalLengthString {

public static String cutString(String s, int length){

if(s.length() < length){//小于截取长度返回全部
return s;
}

if(s.getBytes().length > s.length()){//是中文字符

return s.substring(0, length-1);

}
//英文字符
StringTokenizer st = new StringTokenizer(s," ");

String str = "";

while (st.hasMoreElements()) {

String s1= (String) st.nextElement()+" ";

if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
break;
}
str += s1;
}

return str;

}

public static void main(String[] args) {
String str = "如果一个人轻轻走过,你是否被他的清风带动 ";
String str1 = "if a man came by with you  lightly, whether you would be reflected  by him";

System.out.println(equalLengthString.cutString(str, 10));
System.out.println(equalLengthString.cutString(str1, 10));


}

}


if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
break;
}
这一行如果是英文的话,无法解析,再说了,str你也没有赋值啊
[/Quote]
str是在if后面被赋值的,对吧,呵呵,你拿这个字符串测以下吧,看看效果如何
:Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle;
yunshangcao26 2009-08-25
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 lcj_up 的回复:]
刚刚自己做的,应该可以!!

import java.util.StringTokenizer;
public class equalLengthString {

public static String cutString(String s, int length){

if(s.length() < length){//小于截取长度返回全部
return s;
}

if(s.getBytes().length > s.length()){//是中文字符

return s.substring(0, length-1);

}
//英文字符
StringTokenizer st = new StringTokenizer(s," ");

String str = "";

while (st.hasMoreElements()) {

String s1= (String) st.nextElement()+" ";

if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
break;
}
str += s1;
}

return str;

}

public static void main(String[] args) {
String str = "如果一个人轻轻走过,你是否被他的清风带动 ";
String str1 = "if a man came by with you  lightly, whether you would be reflected  by him";

System.out.println(equalLengthString.cutString(str, 10));
System.out.println(equalLengthString.cutString(str1, 10));


}

}

[/Quote]
if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
break;
}
这一行如果是英文的话,无法解析,再说了,str你也没有赋值啊
lcj_up 2009-08-24
  • 打赏
  • 举报
回复
刚刚自己做的,应该可以!!

import java.util.StringTokenizer;
public class equalLengthString {

public static String cutString(String s, int length){

if(s.length() < length){//小于截取长度返回全部
return s;
}

if(s.getBytes().length > s.length()){//是中文字符

return s.substring(0, length-1);

}
//英文字符
StringTokenizer st = new StringTokenizer(s," ");

String str = "";

while (st.hasMoreElements()) {

String s1= (String) st.nextElement()+" ";

if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
break;
}
str += s1;
}

return str;

}

public static void main(String[] args) {
String str = "如果一个人轻轻走过,你是否被他的清风带动 ";
String str1 = "if a man came by with you lightly, whether you would be reflected by him";

System.out.println(equalLengthString.cutString(str, 10));
System.out.println(equalLengthString.cutString(str1, 10));


}

}
yunshangcao26 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 rascalboy520 的回复:]
怎么到你那里就出异常了,是哪一行?
[/Quote]
好像是输入0还是1啊,我忘了,
yunshangcao26 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cky417 的回复:]
要求中英文标题的长度大致相等,有难度,计算机是不会理解大概的....
[/Quote]
但是它理解误差呀
lsd123 2009-08-24
  • 打赏
  • 举报
回复
.
bigbug9002 2009-08-24
  • 打赏
  • 举报
回复
加一些注释,做一些调整。

    //思路是长度以汉字为准,一个汉字按2算,1个半角字符按1算,都向汉字看起。
//考虑到中英文混用的情况,所以一个字符一个字符拿出来看
//参数lengthAssigned被看做是汉字的个数。
//用totalLeng来记数。

public static String SubStr(String title,int lengthAssigned){

int leng=2*lengthAssigned;
int index=0,totalLeng=0;
boolean hasChinese=false;
while(index<title.length()){
if(title.charAt(index)<256){
totalLeng++;
}else{
totalLeng+=2;
hasChinese=true;
}
if(totalLeng-leng>=0){
break;
}
index++;
}
if(hasChinese){ //如果前面的字符中包含有全角字符,则直接截取字符串。
return index==0?"":title.substring(0,index+1);
}
//在规定长度之内全是半角字符时的处理:
//

if(index==0){
return "";
}
int indexOfSpace=title.lastIndexOf(" ",index+1);
if(indexOfSpace<0){
return title.substring(0,index+1);
}
return title.substring(0,indexOfSpace+1);

}
rascalboy520 2009-08-24
  • 打赏
  • 举报
回复
怎么到你那里就出异常了,是哪一行?
liangwansheng 2009-08-24
  • 打赏
  • 举报
回复

都好厉害啊。。。
yunshangcao26 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 sunnyfun888 的回复:]
引用 20 楼 yunshangcao26 的回复:

String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";
 


越来越复杂了,还带转义字符,呵呵。
[/Quote]


呵呵,标题是任意的
bigbug9002 2009-08-24
  • 打赏
  • 举报
回复
长度为10啊.
bigbug9002 2009-08-24
  • 打赏
  • 举报
回复
F:\java>java Test
如果一个人轻轻走过
if a man came by
UE打开乱码的问题,
Cost-effectiveness,
sunnyfun888 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 yunshangcao26 的回复:]

String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";
 
[/Quote]

越来越复杂了,还带转义字符,呵呵。
yunshangcao26 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 rascalboy520 的回复:]
Java codepublicstatic String subString(String str,int sub) {if (str!=null) {
Pattern pattern= Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]", Pattern.CASE_INSENSITIVE| Pattern.DOTALL);
Matcher m= pattern.matcher(str);if (m.find()) {//中文if (str.length()<= sub) {return str;
}
System.out.print("中文:");
str= str.substring(0, sub)+"...";return str;
}else {
System.out.print("英文:");
sub*=2;if (str.length()<= sub) {return str;
}
String str1= str.substring(0, sub);
String str2= str.substring(sub, str.length());int a= str1.substring(str1.lastIndexOf(""), str1.length()).length();int b= str2.substring(0, str2.indexOf("")).length();
str= a>= b? str.substring(0, sub+ b) : str.substring(0, sub- a);return str+"...";
}
}returnnull;
}publicstaticvoid main(String[] args) {
String str="如果一个人轻轻走过,你是否被他的清风带动";
System.out.println(subString(str,10));
str="if a man came by with you lightly, whether you would be reflected by him";
System.out.println(subString(str,10));
}
这个是加了缩进的
[/Quote]

异常如下:
中文: ...
=========================I:1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1932)
at com.isiteam.TestStr.toLength(TestStr.java:27)
at com.isiteam.Test.main(Test.java:10)
英文:
rascalboy520 2009-08-24
  • 打赏
  • 举报
回复

public static String subString(String str, int sub) {
if (str != null) {
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = pattern.matcher(str);
if (m.find()) {
//中文
if (str.length() <= sub) {
return str;
}
System.out.print("中文: ");
str = str.substring(0, sub) + "...";
return str;
} else {
System.out.print("英文: ");
sub *= 2;
if (str.length() <= sub) {
return str;
}
String str1 = str.substring(0, sub);
String str2 = str.substring(sub, str.length());
int a = str1.substring(str1.lastIndexOf(" "), str1.length()).length();
int b = str2.substring(0, str2.indexOf(" ")).length();
str = a >= b ? str.substring(0, sub + b) : str.substring(0, sub - a);
return str + "...";
}
}
return null;
}
public static void main(String[] args) {
String str = "如果一个人轻轻走过,你是否被他的清风带动";
System.out.println(subString(str, 10));
str = "if a man came by with you lightly, whether you would be reflected by him ";
System.out.println(subString(str, 10));
}

这个是加了缩进的
yunshangcao26 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bigbug9002 的回复:]
Java codepublicclass Test{//public static Timer t=new Timer();publicstaticvoid main(String args[])throws Exception {
String[] titles={"如果一个人轻轻走过,你是否被他的清风带动","if a man came by with you lightly, whether you would be reflected by him"
};for(String title : titles){
System.out.println(SubStr(title,10));
}
}publicstatic String SubStr(String title,int lengthAssigned){int leng=2*lengthAssigned;int index=0,totalLeng=0;boolean hasChinese=false;while(index<title.length()){if(title.charAt(index)<256){
totalLeng++;
}else{
totalLeng+=2;
hasChinese=true;
}if(totalLeng-leng>=0){break;
}
index++;
}if(hasChinese){return title.substring(0,index);
}int indexOfSpace=title.lastIndexOf("",index);return title.substring(0,indexOfSpace);

}
}
F:\java>java Test
如果一个人轻轻走过
if a man came by
[/Quote]

你设置的长度是多少啊,我用另外两个字符串测了以下,youyichang


String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】->【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";

String bb13="Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle; ";
sunnyfun888 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 yunshangcao26 的回复:]
一楼的,当然不好弄了,好弄的话,我早弄出来了

[/Quote]

思路决定问题的难易,换个思路很简单...
加载更多回复(18)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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