连接数组列表中的字符串

发布于 2024-12-01 07:04:17 字数 466 浏览 0 评论 0原文

我有一个数组列表 ArrayList; firstname; 在此我存储了从 xml 文件解析的 n 个名称。

现在,我需要从这个 ArrayList 中获取所有名称并将其存储在一个单独的中 字符串名称以及每个名称之间的斜杠(/)

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

现在我希望它如下名称= a/c/f/g/h/j/k

到目前为止我已经创建了一个for循环来按其大小从ArrayList获取值

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

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 from the ArrayList by its size

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山色无中 2024-12-08 07:04:17
String names = TextUtils.join("/", Appconstant.firstname);
String names = TextUtils.join("/", Appconstant.firstname);
十雾 2024-12-08 07:04:17
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)
      }

尽管您应该使用 StringBuilder 来代替。

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.

若沐 2024-12-08 07:04:17

我的脑海里浮现出这样的事情:

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

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);
救星 2024-12-08 07:04:17
StringBuilder myStrBuilder = null;

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

myStrBuilder.deleteCharAt(myStrBUilder.length()-1) 
StringBuilder myStrBuilder = null;

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

myStrBuilder.deleteCharAt(myStrBUilder.length()-1) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文