我现在要将一系列的中文和英文的标题展示到页面的标题列表中,要求中英文标题的长度大致相等,并且英文不允许从一个单词中间截开,
比如:如果标题为如下两个:
(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);
在线等,急用,
比如:如果标题为如下两个:
(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);
在线等,急用,
解决方案 »
- Tomcat问题
- 测试web service的页面的参数输入处出现java.util.List is an unsupported type错误
- 讨教习惯问题
- 求一个杨辉三角形的代码(不用面向对象的前提下)
- 小鸟求救!!急急急!!写一个关于监控IP包流量的系统,高手们指点下!
- swing是不是你目前所见在工业界最优雅和最灵活的GUI框架呢?
- 关于内存的搭配问题:)
- 请问一下问题,如何把数12365496改成标准记数法,比如123,654.96
- PreparedStatement的Batch问题???急!!
- 改哪里可以显示数据库中的表
- JXTA管道通告搜索问题求助
- 求设备监控程序(如电网设备监控)的实现方案
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;
}
str = str.substring(0, sub);
str = str.substring(0, str.lastIndexOf(" ")) + "...";
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));
}
中文: 如果一个人轻轻走过,...
英文: if a man came by...
//public static Timer t=new Timer();
public static void 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));
}
} 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 title.substring(0,index);
}
int indexOfSpace=title.lastIndexOf(" ",index);
return title.substring(0,indexOfSpace);
}
}
F:\java>java Test
如果一个人轻轻走过
if a man came by
TestString ts = new TestString();
String s = "if a man came by with you lightly, whether you would be reflected by him";
System.out.println(ts.SubStr(s, 20));
}
String SubStr(String title, int lengthAssigned){ //英文
int len = title.length();
if(len <= lengthAssigned){
return title;
}
String temp = title.substring(0, lengthAssigned);
if(" ".equals(title.substring(lengthAssigned, lengthAssigned + 1))){
return temp + "...";
}
int sawp = temp.lastIndexOf(" ");
return temp.substring(0,sawp) + "...";
}
}只做了英文的....
<div style="font-size: 12pt;">
<span style="width:180; height:18; overflow : hidden; word-break : keep-all">
如果一个人轻轻走过,你是否被他的清风带动 </span>...</div>
<div style="font-size: 12pt;">
<span style="width:180; height:18; overflow : hidden; word-break : keep-all">
if a man came by with you lightly, whether you would be reflected by him </span>...</div>
程序有异常!!!!!!!!!!!!!!!!!!!!!!!
=========================sub:1
英文: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1938)
at com.isiteam.TestStr.toLength(TestStr.java:26)
at com.isiteam.Test.main(Test.java:10)
你设置的长度是多少啊,我用另外两个字符串测了以下,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; ";
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));
}这个是加了缩进的
异常如下:
中文: ...
=========================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)
英文:
如果一个人轻轻走过
if a man came by
UE打开乱码的问题,
Cost-effectiveness,
//考虑到中英文混用的情况,所以一个字符一个字符拿出来看
//参数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);
}
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));
}}
break;
}
这一行如果是英文的话,无法解析,再说了,str你也没有赋值啊
:Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle;
这是我的测试代码:
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));
}