开发者

Concating a String from an arraylist

I have an array list ArrayList<String> firstname; In this I am storing n number of names which have been parsed from an xml file.

Now from this ArrayList I need to take all the names and store it in a Single separate String names along with a slash(/) between of each names.

For eg firstname= {a, c,f,g,h,j,k}

Now i want it to be as follows names= a/c/f/g/h/j/k

So far I have created a for loop to get values 开发者_如何学Cfrom the ArrayList by its size

String names;
 for(int k=0;k<Appconstant.firstname.size();k++) {
    names = Appconstant.firstname.get(k);
 }


String names = TextUtils.join("/", Appconstant.firstname);


String names;
 for(int k=0;k<Appconstant.firstname.size();k++)
       {
        if(k<=Appconstant.firstname.size()-1)
            names += Appconstant.firstname.get(k)+"/";
        else
           names += Appconstant.firstname.get(k)
      }

Though you should be using a StringBuilder instead.


Off the top of my head, but something like this:

StringBuilder builder = new StringBuilder();
for(String name : firstname)
{
    builder.append(name).append('/');
}
// Remove last '/'
builder.deleteCharAt(builder.length()-1);


StringBuilder myStrBuilder = null;

for(String aName:yourNameList)
{
  myStrBUilder.append(aName+"/");
}

myStrBuilder.deleteCharAt(myStrBUilder.length()-1) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜