view raw
yshak yshak - 11 months ago 83
Java Question

Trim a string based on the string length

I want to trim a string if the length exceeds 10 characters.

Suppose if the string length is 12 (

String s="abcdafghijkl"
), then the new trimmed string will contain
"abcdefgh.."
.

How can I achieve this?

Answer
s = s.substring(0, Math.min(s.length(), 10));