如果我的字符串太长,我想把它分割成两个子串并显示出来。
这是我使用的代码:
if(newQuestion.length() > 35){
String first_part = newQuestion.substring(0,newQuestion.length()/2);
String second_part = newQuestion.substring(newQuestion.length()/2);
question1.setText(first_part + "\n" + second_part);
}
else{question1.setText(newQuestion + "");}其结果是:
青铜时代沉淀者 在印度北部。
如果总字符串长度> 35,但不拆分实际单词,则如何拆分它,因为它不是很好。
发布于 2015-12-10 10:45:22
空间分裂。然后将单词添加到第一个字符串中,直到字符串的长度为35个字符,然后将剩下的单词添加到2.string中。但是听起来question1视图应该能够自己处理这个问题吗?
String newQuestion = "Bronze age settlements in northern India.";
String[] words = newQuestion.split(" ");
String line1 = "";
String line2 = "";
int size = 0;
for(int i = 0; (line1.length() + words[i].length()) < 25; i++) {
size++;
line1 += words[i] + " ";
}
for(int j = size; j < words.length; j++) {
line2 += words[j] + " ";
}
System.out.println(line1);
System.out.println(line2);https://stackoverflow.com/questions/34199649
复制相似问题