可能重复:
up to first N characters
I need to “trim” my variable called: “TITLE” to only 40 characters
Java:限制/修剪/删除字符串中的额外字符。我希望在字符串变量的40个字符后删除任何多余的字符。
答案 0 :(得分:2)
String shortString = longString.substring(0, Math.min(longString.length(), 40));
您可能希望仔细查看String类