在给定这些条件的情况下,我试图在不同的行中返回字符串。由于我不能在 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;
}