java - 如何在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,
请给出指示并纠正错误所在。
解决方案
您不仅需要将“,”连接到“as”,还需要将“as”的值连接到结果。否则它将只包含最后分配的值。
推荐阅读
- nginx - NGINX 如果它是正则表达式,如何将子域重写为子目录?
- apache-spark - Spark 1.6中的UDF重新分配给val错误
- c++ - c++ 致命错误:glad/gl.h: No such file or directory
- java - Mockito for Spring JPA Specification API 不完全执行动态查询
- r - 从 gfortran 调用 Lapack DLL
- maven - 在 Anypoint 平台 (CloudHub) 中使用 Jenkins 部署时出现错误 409 冲突
- sql-server - 如何通过 SQL Server 插入的索引有效地替换长字符串?
- javascript - 如何使用 javascript 或 jquery post 方法从循环中发送变量并使用 php 接收
- r - 如何在 gitbook 风格的 bookdown 中使用 gitbook 插件
- macos - macOS Catalina 内核调试