0

在给定这些条件的情况下,我试图在不同的行中返回字符串。由于我不能在 Java 中将 += 与字符串一起使用,我如何制作一个每行间隔但“堆栈”的巨型字符串?换句话说,如何将循环中的新字符串添加到旧字符串?

/**
   Returns a String that concatenates all "offending"
   words from text that contain letter; the words are
   separated by '\n' characters; the returned string
   does not contain duplicate words: each word occurs
   only once; there are no punctuation or whitespace
   characters in the returned string.

   @param letter character to find in text
   @return String containing all words with letter
 */
public String allWordsWith(char letter)
{
    String result = "";

    int i = 0;
    while (i < text.length())
    {
        char newchar = text.charAt(i);
        if (newchar == letter)
        {
            int index1 = text.lastIndexOf("",i);
            int index2 = text.indexOf("",i);
            String newstring = '\n' + text.substring(index2,index1);
        }
        i++;
    }
    return result;
}
4

5 回答 5

1

修改result字符串,并修复您的“单词边界”测试。

if (newchar == letter) {
    int index1 = text.lastIndexOf(' ',i);
    int index2 = text.indexOf(' ',i);
    // TODO -- handle when index1 or index2 is < 0;  that means it wasn't found, 
    //  and you should use the string boundary (0 or length()) instead.
    String word = text.substring( index2,index1);
    result += "\n" + word;
}

如果您真的关心性能,您可以使用StringBuilderand append(),但除此之外,我强烈赞成+=简洁易读。

于 2013-10-09T05:10:10.983 回答
1

您每次都在循环中重新初始化您的字符串。将字符串声明移出 eof 循环:

替换这个

        String newstring = '\n' + text.substring(index2,index1);

        result = '\n' + text.substring(index2,index1);
于 2013-10-09T05:06:51.180 回答
0

首先,使用 StringBuilder。

其次,使用 System.getProperty("line.separator") 确保使用正确的换行符。

编辑代码:

public String allWordsWith(char letter)
{
    StringBuilder sb = new StringBuilder();

    int i = 0;
    while (i < text.length())
    {
        char newchar = text.charAt(i);
        if (newchar == letter)
        {
            int index1 = text.lastIndexOf("",i);
            int index2 = text.indexOf("",i);
            sb.Append(text.substring(index2,index1));
            sb.Append(System.getProperty("line.separator"));
            //I put the new line after the word so you don't get an empty 
            //line on top, but you can do what you need/want to do in this case.
        }
        i++;
    }
    return result;
}
于 2013-10-09T05:16:58.683 回答
0

Use StringBuilder as following:

public String allWordsWith(char letter){
 //String result = ""; 
 StringBuilder result = new StringBuilder();
 int i = 0;
 while (i < text.length()){
    char newchar = text.charAt(i);
    if (newchar == letter){
        int index1 = text.lastIndexOf("",i);
        int index2 = text.indexOf("",i);
        result.append('\n' + text.substring(index2,index1));
    }
    i++;
 }
 return result.toString();
}
于 2013-10-09T05:18:48.497 回答
0
String text = "I have android code with many different java, bmp and xml files everywhere in my project that I used during the drafting phase of my project.";
String letter = "a";
Set<String> duplicateWordsFilter = new HashSet<String>(Arrays.asList(text.split(" ")));
StringBuilder sb = new StringBuilder(text.length());
for (String word : duplicateWordsFilter) {
    if (word.contains(letter)) {
        sb.append(word);
        sb.append("\n");
    }
}
return sb.toString();

结果是:

android
have
java,
drafting
and
many
that
phase
于 2013-10-09T05:34:12.040 回答