How do I get up to the first n characters of a string in Java without doing a size check first (inline is acceptable) or risking an IndexOutOfBoundsExcept
n
IndexOutOfBoundsExcept
String upToNCharacters = String.format("%."+ n +"s", str);
Awful if n is a variable (so you must construct the format string), but pretty clear if a constant:
String upToNCharacters = String.format("%.10s", str);
docs