2014-01-21 31 views
0

初學程序員在這裏。如何用逗號和空格分隔字符串而不將它們放在它之前?

我不得不追加文件名的字符串連接在一起,並拿出了以下方法:

class ConsLoItem implements ILoItem { 
    Item first; 
    ILoItem rest; 

    ConsLoItem(Item first, ILoItem rest) { 
     this.first = first; 
     this.rest = rest; 
    } 

public String images() { 
    if (this.rest.equals(new MtLoItem())) 
    { 
     return this.first.images(); 
    } 
    else 
     return this.first.images() + ", " + this.rest.images(); 
    } 
} 

,並繼續得到同樣的結果,具有:

「 jesse.jpeg,」當我期待「jesse.jpeg」。

關於如何從文件名的開始和結尾刪除「,」的任何想法?

+0

檢查:http://stackoverflow.com/questions/2088037/trim-characters-in-java 你可以使用的replaceAll( )方法 –

+0

http://stackoverflow.com/questions/6652687/strip-leading-and-trailing-spaces-from-java-string – FuriousGeorge

+0

顯示類MtLoItem的def。 –

回答

0

你可以改變你images方法,像這樣

public String images() { 
    if (this.rest.equals(new MtLoItem())) { 
     return this.first.images(); 
    } else { // missed a brace here. 
     if (this.first.images().length() > 0) { 
      return this.first.images() + ", " + this.rest.images(); 
     } 
     return this.rest.images(); 
    } 
} 
相關問題