2016-04-09 7 views
0

私のメソッド実行後に "、" - カンマを削除しようとしています。メソッドの後に誰かが "、"を取り除く方法を教えてくれますか?コンマメソッドを削除するjava

String.replaceAll(",", "\n"); 

またはあなたの場合:

public String toString(){ 
    String bob =""; 
    for(Person p : bcc){ 
     bob+= p.getName() + " is friends with "; 
     for(Person fOfp : p.getFriendList()){ 
      bob+= fOfp.getName() +", "; 
     } 
     bob+="\n"; --i want to add here to remove the " , " 
    } 

    return bob; 
} 

答えて

0

文字列クラスは、そう、あなたの文字列でなければならないすべては、そのための方法を持っている

bob.replaceAll(",","\n"); 
0

削除しないでください。カンマを付けてから追加します。最後に追加しない方がいいです。この方法で実現できます。

public String toString(){ 
    String bob =""; 
      for(Person p : bcc){ 
       bob+= p.getName() + " is friends with "; 
       String comma=""; 
       for(Person fOfp : p.getFriendList()){ 
        bob+=comma; 
        bob+= fOfp.getName(); 
        comma=", "; 
       } 
       bob+="\n"; 
      } 

    return bob; 
} 

また、Stringには追加しないでください。 StringBuiderの方がはるかに高速で、メモリ消費も少なくて済むので、StringBuiderの方が使いやすくなります。