2013-10-09 67 views
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; 
} 
+0

关于使用StringBuilder的,这样就可以使用什么追加方法? – midhunhk

+2

为什么你不能使用'+ ='?当然,使用'StringBuilder'更合理,但干草... – MadProgrammer

+0

在上面的代码中,变量'newstring'在哪里使用。我认为用result ='\ n'+替换该行将会解决您的问题 – midhunhk

回答

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; 
} 

如果你真的关心性能,你可以使用一个StringBuilderappend(),但否则我坚决赞成+=为更加简洁&可读。

1

你每次都在重新初始化你的字符串。移动字符串声明outsid EOF循环:

替换此

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

 result = '\n' + text.substring(index2,index1); 
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; 
} 
0

使用StringBuilder如下:

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(); 
} 
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