yshak yshak - 1 year ago 132
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

How can I achieve this?

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