java替换字符

zy0402 2009-07-24 02:51:08
一串字符,只显示前12位,后面用...表示,请问如何实现
...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aa870816 2009-07-25
  • 打赏
  • 举报
回复
楼主你自己加个判断不就行了~~~~
hqq592440660 2009-07-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bingsha1976 的回复:]
public static void main(String[] args) {
String str="abcdefghijklmnopq";

System.out.println(str.substring(0,12)+"...");
}
[/Quote]

如果我的字符串没有12位,岂不是会报错?
hello123qwe 2009-07-25
  • 打赏
  • 举报
回复
String str="aassdddd";
int leng=str.length();
if(leng<12)
{
System.out.println(str.substring(0,length)+"...");
}else
{
System.out.println(str.substring(0,12)+"...");
}
cwjieNo1 2009-07-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hello123qwe 的回复:]
String str="aassdddd";
int leng=str.length();
if(leng <12)
{
  System.out.println(str.substring(0,length)+"...");
}else
{
  System.out.println(str.substring(0,12)+"...");
}
[/Quote]
这个很好啊`
bingsha1976 2009-07-24
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String str="abcdefghijklmnopq";

System.out.println(str.substring(0,12)+"...");
}

62,621

社区成员

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

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