I would go with something similar to the standard model that you have. I wouldn’t worry about the characters, as @Gopi said this was probably the end of the balance. What I'm doing is new, has another parameter called "minNumberOfhiddenCharacters" (maybe a little less verbose). Then, when you set the ellipsis, I will do something like:
if (text.length() > length+minNumberOfhiddenCharacters) { return text.substring(0, length - 3) + "..."; }
What this will mean is that if the length of your text is 35, your length will be 30, and the minimum number of characters to hide is 10, then you will get a full line. If your minimum character number to hide is 3, you should get an ellipsis instead of these three characters.
The main thing you need to know is that I distorted the value "length" so that it is no longer the maximum length. The length of the output string can now be from 30 characters (if the text length> 40) to 40 characters (if the text length is 40 characters). Effectively our maximum length becomes length + minNumberOfhiddenCharacters. A string, of course, can be shorter than 30 characters if the original string is less than 30, but this is a boring case that we should ignore.
If you want the length to be stiff and fast, then you need something more:
if (text.length() > length) { if (text.length() - length < minNumberOfhiddenCharacters-3) { return text.substring(0, text.length() - minNumberOfhiddenCharacters) + "..."; } else { return text.substring(0, length - 3) + "..."; } }
So, in this example, if text.length () is 37, the length is 30, and minNumberOfhiddenCharacters = 10, then we will go into the second part of the internal if and get 27 characters + ... to make 30. This is actually the same as if we entered the first part of the cycle (which is a sign, we have our boundary conditions on the right). If the text length was 36, we would get 26 characters + ellipsis, giving us 29 characters with 10 hidden.
I discussed whether the restructuring of some comparison logic could make it more intuitive, but in the end I decided to leave it the way it is. You may find that text.length() - minNumberOfhiddenCharacters < length-3 makes what you do more obvious though.