java 拼接字符和解析字符串
发布网友
发布时间:2022-04-20 14:42
我来回答
共2个回答
热心网友
时间:2023-06-25 22:52
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015/6/14.
*/
public class Test {
private static List<String> infor;
static {
infor = new ArrayList<String>();
infor.add("流水号1");
infor.add("收款方帐号1");
infor.add("真实姓名");
infor.add("付款金额1");
infor.add("备注说明1");
infor.add("流水号2");
infor.add("收款方帐号2");
infor.add("真实姓名");
infor.add("付款金额2");
infor.add("备注说明2");
}
public static void main(String args[]) {
/**
* 开始拼接字符串
*/
String inforString = infor.get(0);
for (int i = 1, k = infor.size(); i < k; i++) {
if (i % 5 == 0) {
inforString += "|";
} else {
inforString += "^";
}
inforString += infor.get(i);
}
System.out.println("拼接后的字符串是:" + inforString);
/**
* 开始解释字符串
*/
String[] usersInfor = inforString.split("\\|");
System.out.println(usersInfor.length);
User user;
String[] userString;
for (int i = 0, k = usersInfor.length; i < k; i++) {
userString = usersInfor[i].split("\\^");
user = new User();
user.setId(userString[0]);
user.setGathering(userString[1]);
user.setName(userString[2]);
user.setPayment(userString[3]);
user.setRemark(userString[4]);
System.out.println("User" + i + "=" + user);
}
}
static class User {
private String id;
private String gathering;
private String name;
private String payment;
private String remark;
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGathering() {
return gathering;
}
public void setGathering(String gathering) {
this.gathering = gathering;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPayment() {
return payment;
}
public void setPayment(String payment) {
this.payment = payment;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "[id:" + id + ",gathering:" + gathering + ",name:" + name + ",payment:" + payment + ",remark:" + remark + "]";
}
}
}
热心网友
时间:2023-06-25 22:53
String str="";
for(String item : list){
str=str+item+",";
}
str=str.substring(0,str.length-1);
以上代码可以将list中存储的字符串以逗号拼接起来,解析字符串的时候以逗号为分隔符进行分割产生字符串数组,代码为:
String[] strs=str.split(",");
不知道这是不是你需要的,希望能帮到你
追问跟我的要求不相同啊!!!