package com.tfj.demo;
/**
* @date 2015年12月17日 下午8:57:08
*/
public class Test2 {
public static String StringTruncat(String oldStr, int maxLength, String endWith) {
// 判断原字符串是否为空
if (oldStr.equals(""))
return oldStr + endWith;
// 返回字符串的长度必须大于1
if (maxLength < 1)
System.out.println("请将返回的字符串长度设置成大于0");
// 判断原字符串是否大于最大长度
if (oldStr.length() > maxLength) {
// 截取原字符串
String strTmp = oldStr.substring(0, maxLength);
// 判断后缀是否为空
if (endWith.equals(""))
return strTmp + "...";
else
return strTmp + endWith;
}
return oldStr;
}
public static void main(String[] args) {
System.out.println(Test2.StringTruncat("这个链接时某某某在某某某网站分享的某某某文件,有兴趣的可以自行下载查看,不喜勿喷,谢谢", 16,
"..."));
}
}
![显示内容中的一部分 显示内容中的一部分](https://images2015.cnblogs.com/blog/512541/201512/512541-20151217210913084-229577255.png)
maxLength设置为0时
![设置为零时 设置为零时](https://images2015.cnblogs.com/blog/512541/201512/512541-20151217210913677-1111486130.png)