Abbreviate string in Java
Description
The following code shows how to abbreviate string.
Example
// w ww . j a v a 2 s . c om
public class Main {
public static void main(String[] argv) {
System.out.println(abbreviate("this is a test from java2s.com", 10));
}
public static String abbreviate(String str, int maxWidth) {
if (null == str) {
return null;
}
if (str.length() <= maxWidth) {
return str;
}
return str.substring(0, maxWidth) + "...";
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »