我有一个分隔字符串,分隔符是“$%”。这些字段按特定顺序排列。例如:约翰$%史密斯$%30$%洛杉矶 我需要获取该字符串的值并将它们存储在相应的属性中
Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)
到目前为止,我已经尝试过这个,但我知道我做错了:
if(thisString != null){
if(thisString.endsWith("$%")){
Customer.firstName(thisString.substring(0,indexOf("$%");
}
}
最佳答案
尝试使用 Java 的 String.split
例如:
//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc
关于java - 如何从分隔字符串中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837986/