你可以尝试这样的事情(如果你想第一个50个字):
String explanation="The image-search feature will start rolling out in the next few days, said Johanna Wright, a Google search director. "Every picture has a story, and we want to help you discover that story she said."
String[] words = explanation.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Math.min(50, words.length); i++)
{
sb.append(words[i] + " ");
}
System.out.println("The first 50 words are: " + sb.toString());
或像这样的东西,如果你想要的前50个字符:
String explanation="The image-search feature will start rolling out in the next few days, said Johanna Wright, a Google search director. "Every picture has a story, and we want to help you discover that story she said."
String truncated = explanation.subString(0, Math.min(49, explanation.length()));
你想前50个字,还是前50个字? – Mob
你有什么尝试?你认为JavaDoc中有什么你认为可能有用的'String'类吗? –
您的示例中显示的字符串中有35个单词和199个字符。没有35也没有199等于300. – DwB