如何在不切断单词的情况下自动将一行分成多行?每个新行的长度大约是4个字?我有很多句子因此我不能使用\n
e.g:
If I were you I would go to the cinema with her
变为:
If I were you
I would go to
the cinema with her
希望尽快看到你的帮助。谢谢!
答案 0 :(得分:1)
我想,根据你所提出的内容,虽然我不确定你是否正在考虑所有可能的案例,但是在获取一些东西的同时获得你正在寻找的具体答案的方法理所当然而不是直接依赖“\ n”将是......
String s = "If I were you I would go to the cinema with her";
String[] strings = s.split(" ");
for(int i = 0; i < strings.length; ++i) {
if(i % 4 == 0) {
System.out.println();
}
System.out.print(strings[i] + " ");
}
或者你可以考虑这样的东西,它会处理文本字段的最大宽度而不是一定数量的单词,因为有些单词可能很长并导致你试图避免的情况......
int MAX = 20;
int length = 0;
String s = "If I were you I would go to the cinema with her.";
String[] strings = s.split(" ");
for(int i = 0; i < strings.length; ++i) {
if((length + strings[i].length()) > MAX ) {
System.out.println();
length = 0;
}
System.out.print(strings[i] + " ");
length += strings[i].length() + 1;
}
修改强>
我按你的要求做了。这是我从MAX选项得到的......
If I were you I
would go to the
cinema with her and
abc xyz
这就是我为常规所得到的......
If I were you
I would go to
the cinema with her
and abc xyz
不确定那里发生了什么,但我会说我在我的答案上跳了鲨鱼。你已经标记了Android,你和我都知道System.out.println()
在这种环境中是禁忌,至少如果你希望看到任何结果。对不起。
答案 1 :(得分:0)
你需要计算for循环中的空格数这里是一个代码来演示它。 请根据您的应用更改变量
String tv2 = tv.getText().toString(); // take a string textVIew, you can make it editView
StringBuilder sb = new StringBuilder(tv2); // add the string to stringBuilder
int howManySpaces = 0; // this for counting the spaces.
for (int i = 0; i < tv2.length(); i++)
{
if (tv2.charAt(i) == ' ') //if space found add one to howManySpaces
{
howManySpaces += 1;
Log.d("HMS", String.valueOf(howManySpaces));
}
if (howManySpaces == 4) // if howManySpaces == 4 break it to new line
{
sb.replace(i, i+1, "\n");
howManySpaces = 0;
}
}
tvNew.setText(sb.toString()); // add to the new textView the result after breaking.
我刚刚尝试过,用相同的句子给了我想要的结果。
如果你不理解任何部分,请随时问我。答案 2 :(得分:0)
我已经尝试了以下代码,它对我来说很好,请试试这个,如果你有任何麻烦,请告诉我
//调用SentenceBreaker方法,帮助String拆分。
sentenceBreaker("If I were you I would go to the cinema with her");
//传递句子的方法
private void sentenceBreaker(int noOfWords,String inputSentence){
boolean previousCharWhiteSpace = true; // just a flag
boolean initialFlag =false;
int wordCount = 0;
int i,count =0;
for (i = 0; i < inputSentence.length(); i++) {
if (inputSentence.charAt(i) == ' ' && !previousCharWhiteSpace) {
wordCount++;
previousCharWhiteSpace = true;
if (wordCount == noOfWords) {
if(count == 0){
inputSentence = inputSentence.substring(0,wordCount)
+ "\n"
+ inputSentence.substring(wordCount,
inputSentence.length());
wordCount = 0;
count=i;
}
else{
inputSentence = inputSentence.substring(count, i)
+ "\n"
+ inputSentence.substring(i,
inputSentence.length());
wordCount = 0;
count=i;
}
}
} else if (!(inputSentence.charAt(i) == ' ')) {
previousCharWhiteSpace = false;
}
}
/*
* the for loop increments the word count if a space is encountered
* between words,for multiple spaces between words it wont update the
* counter-hence the use of the boolean flag.
*/
if (!(inputSentence.charAt(i - 1) == ' ')) {
wordCount++;
}
// just to make sure that we count the last word in the sentence as well
System.out.println("No of words-" + wordCount);
System.out.println("Sentence" + inputSentence);
}
/ *输出* /
句子如果我是你
我会去
电影院与她**
答案 3 :(得分:0)
根据您的要求。以下逻辑将正常工作。请使用它
String stT="If I were you I would go to the cinema with her";
String[] sT=stT.split(" ");
StringBuffer sb=new StringBuffer();
for(int i=0;i<sT.length;i++)
{
if(i%4==3)
sb.append(sT[i]+"\n");
else
sb.append(sT[i]+" ");
}
System.out.print(sb.toString());