by
5,032 33
0
4
2,577
0
Top 1% !
Popular
Famous
Specified
Popularity: 5441st place
Created
Modified Oct 7, 2013
This snippet is Public
No tags for this snippet yet.
Architecturex86_64
InputName0input
InputName1maxLen
InputType0String
InputType1int
JVMjre6
LanguageJava
LicenseMIT_X11
MethodNametrimWithDots
OSWindows
OutputTypeString

Trim a string with ellipsis

This is the simplest way to trim a string to the given length.
If trimmed, the last part of the string is replaced with ellipsis
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/11318/Trim-a-string-with-ellipsis?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static String trimWithDots(String input, int maxLen) { if (isNullOrEmpty(input)) return ""; if (input.length() > (maxLen - 3)) return input.substring(0, maxLen - 3) + "..."; else return input; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus