英文为什么跟中文占同样的字符?

peterwu0126 2004-11-01 02:26:22
我的页面上控制每条新闻取前10个字
但是为什么英文一个字母也算1个字
这样,使得页面不整齐
不知道为什么
String sql2="select * from question order by qtime desc limit 7 ";
ResultSet rs2=connBean.executeQuery(sql2);
while(rs2.next())
{
String id2=rs2.getString("id");
String content=rs2.getString("content");
if(content.length()>10)
{
content = content.substring(0, 10);
content += "...";
}
%>
...全文
243 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
peterwu0126 2004-11-11
  • 打赏
  • 举报
回复
再顶
请大虾们帮忙 。
我写的调用调不出来
提示C:\webserv\Tomcat\work\Standalone\localhost\wlzx\index_jsp.java:785: getLimitedStr(java.lang.String,int) in wlzx.HTMLencode cannot be applied to (java.lang.String)
title=htmlcode.getLimitedStr(title);
^
peterwu0126 2004-11-10
  • 打赏
  • 举报
回复
能写个调用的例子吗?
谢谢了!
JoeChen 2004-11-10
  • 打赏
  • 举报
回复
测试了一下有问题改一下:
public String getLimitedStr(String str,int maxLimit){
String limitString=str;
if (str == null) {
return null;///改动
}

if (str.getBytes().length <= maxLimit) {
limitString = str;
}else{
byte[] bytes;
char tempChar;
int count = 0, index = 0;
while (count < maxLimit) {
limitString += str.charAt(index);
tempChar = str.charAt(index);
index++;
count++;
if (tempChar>'\u00ff'){
count += 1;
}
if (count > maxLimit){
limitString = limitString.substring(0, index - 1);
}
}
}
return limitString + "...";

}
JoeChen 2004-11-10
  • 打赏
  • 举报
回复
呵呵~~,很好用啊!谢谢...函数写全一下:
public String getLimitedStr(String str,int maxLimit){
String limitString=str;
if (str == null) {
limitString=str;
}

if (str.getBytes().length <= maxLimit) {
limitString = str;
}else{
byte[] bytes;
char tempChar;
int count = 0, index = 0;
while (count < maxLimit) {
limitString += str.charAt(index);
tempChar = str.charAt(index);
index++;
count++;
if (tempChar>'\u00ff'){
count += 1;
}
if (count > maxLimit){
limitString = limitString.substring(0, index - 1);
}
}
}
return limitString + "...";

}
peterwu0126 2004-11-10
  • 打赏
  • 举报
回复
如果要用bean呢?
应该怎么引用呢?
麻烦大侠
赫赫。。
crazy_he 2004-11-09
  • 打赏
  • 举报
回复
对不起,第一行改成 String limitString=str;//str为你要截取的字符串
crazy_he 2004-11-09
  • 打赏
  • 举报
回复
String limitString="str";
if (str == null) {
limitString=str;
}

if (str.getBytes().length <= maxLimit) {
limitString = str;

}
else {
byte[] bytes;
char tempChar;
int count = 0, index = 0;
while (count < maxLimit) {
limitString += str.charAt(index);
tempChar = str.charAt(index);
if (tempChar <= '\u00ff') {
index++;
count++;
}
else {
index++;
count += 2;
}
if (count > maxLimit) {
limitString = limitString.substring(0, index - 1);
}
}
}
limitString=limitString + "...";

out.println(limitString);
crazy_he 2004-11-09
  • 打赏
  • 举报
回复
例如:你的String是str。
String str1="str";
if (str == null) {
str1=str;
}

if (str.getBytes().length <= maxLimit) {
limitString = str;
return limitString;
}
else {
byte[] bytes;
char tempChar;
int count = 0, index = 0;
while (count < maxLimit) {
limitString += str.charAt(index);
tempChar = str.charAt(index);
if (tempChar <= '\u00ff') {
index++;
count++;
}
else {
index++;
count += 2;
}
if (count > maxLimit) {
limitString = limitString.substring(0, index - 1);
}
}
}
return limitString + "...";
}

wuyuestar 2004-11-09
  • 打赏
  • 举报
回复
把你显示地方的source也贴出来吧.
peterwu0126 2004-11-09
  • 打赏
  • 举报
回复
具体怎么调用阿
我挺菜的,
请大侠们明示
cmg7758 2004-11-08
  • 打赏
  • 举报
回复
(天平)的方法没问题,你最好把错误贴出来!
hellenlong 2004-11-08
  • 打赏
  • 举报
回复
limit 7请教一下,这个limit 7有什么用
peterwu0126 2004-11-08
  • 打赏
  • 举报
回复
自己再顶一下,请大家帮忙!
crazy_he 2004-11-08
  • 打赏
  • 举报
回复
用我的方法就行了,不用javabeans也可以,你把字符串通过那个函数输出就行了
peterwu0126 2004-11-08
  • 打赏
  • 举报
回复
to cmg7758(~逛逛~)
我上面说了,没有报错
但是多于10字的新闻
就都变成“。。。”了
根本没显示前10字的内容
crazy_he 2004-11-08
  • 打赏
  • 举报
回复
把这段写在javabeans里调用就行了
crazy_he 2004-11-08
  • 打赏
  • 举报
回复
限制字符串的输出
/**
*
* @param str 输入的字符串
* @param maxLimit 规定限制长度大小(汉字2位,字母数字一位)
* @return 限制后的字符串
*/
public String length_limit(String str, int maxLimit) {
String limitString = "";
if (str == null) {
return str;
}

if (str.getBytes().length <= maxLimit) {
limitString = str;
return limitString;
}
else {
byte[] bytes;
char tempChar;
int count = 0, index = 0;
while (count < maxLimit) {
limitString += str.charAt(index);
tempChar = str.charAt(index);
if (tempChar <= '\u00ff') {
index++;
count++;
}
else {
index++;
count += 2;
}
if (count > maxLimit) {
limitString = limitString.substring(0, index - 1);
}
}
}
return limitString + "...";
}
peterwu0126 2004-11-04
  • 打赏
  • 举报
回复
请各位帮忙看看
yanjianjiang 2004-11-02
  • 打赏
  • 举报
回复
title1=new String(title1.getBytes(),0,16);
------------------------------------------------
这句的分号错了!
peterwu0126 2004-11-02
  • 打赏
  • 举报
回复
不行,
报错
是不是写法有问题
while(rs.next())
{
String id1=rs.getString("id");
String title1=rs.getString("title");
String pubtime=rs.getString("pubtime");
if(title1.length()>16)
{
title1=new String(title1.getBytes(),0,16);
// title1= title1.substring(0, 16);
title1 += "...";
}


Generated servlet error:
[javac] Compiling 1 source file

C:\webserv\Tomcat\work\Standalone\localhost\wlzx\index_jsp.java:656: illegal character: \65307
加载更多回复(2)

81,122

社区成员

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

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