首页 > 解决方案 > 如何在Java的循环中连接2个或更多字符串

问题描述

我在将两个字符串或多个字符串连接为 java 循环中的一个变量时遇到问题。

我的预期结果如下:

/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml

结果保存在一个变量中并返回如下值:

/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml

我实施的以下内容:

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class pathData {

    public static void main(String[] args) throws Exception {   
        
        String pthData = "\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710002.xml,\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710003.xml";
        String bk = splitPath(pthData);
        System.out.println(bk);
        
    }
    
    private static String splitPath(String bk) throws Exception {
        String[] Str = bk.split(",");
        String result = "";

        if(Str.length > 1) {
            for(int i = 0; i < Str.length; i++) {
                String[] ss = Str[i].split("\\\\");
                int a = ss.length;
                String as = "/AIP"+"/"+ss[a-4]+"/"+ss[a-3]+"/"+ss[a-2]+"/"+ss[a-1];
                
                result = as.concat(",");;
            }           
            return result;      
        }   
        return "";  
    }
}

根据我的代码,结果总是返回这样的值

/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml,

请给出指示并纠正错误所在。

标签: javaspringspring-bootfor-loopspring-mvc

解决方案


您不仅需要将“,”连接到“as”,还需要将“as”的值连接到结果。否则它将只包含最后分配的值。


推荐阅读