Java 如何将字符串值拆分为多个值

Java 如何将字符串值拆分为多个值,java,Java,我有一个字符串值,我从一个svo中获取, i、 e.String reply=svo.getreply() 我得到的输出像-->“1:true,2:false,3:true,4:false,5:false,6:false” 现在,我想要的是分离回复的存储,并将所有回复存储在每个回复的新变量中。例如: String firstVal= "true"; String secondeVal= "false"; // ... and so on. 我该怎么做呢?这在java中是不可能的,除非您使用f

我有一个
字符串
值,我从一个svo中获取, i、 e.
String reply=svo.getreply()

我得到的输出像-->
“1:true,2:false,3:true,4:false,5:false,6:false”

现在,我想要的是分离回复的存储,并将所有回复存储在每个回复的新变量中。例如:

String firstVal= "true";
String secondeVal= "false";
// ... and so on. 

我该怎么做呢?

这在java中是不可能的,除非您使用for循环,否则无论如何都可以使用
String#split(“,”)
要将字符串拆分为字符串[]

可以从此字符串生成
映射。然后根据需要使用该地图。
例如:
stringfirstval=map.get(1)

String s1=“1:true,2:false,3:true,4:false,5:false,6:false”;
Map Map=newhashmap();
对于(字符串s:s1.split(“,”)){
put(Integer.parseInt(s.substring(0,s.indexOf(“:”)),s.substring(s.indexOf(“:”)+1));
}
for(整型键:map.keySet())System.out.println(键+“”+map.get(键));

我使用地图界面为您编写了一些示例代码:

public class splitstringexample {
    public static void main(String[] args) {
        String reply = "1:true,2:false,3:true,4:false,5:false,6:false";
        Map<String, Boolean> example = splitString(reply);

        for (String name: example.keySet()){
            String key =name.toString();
            String value = example.get(name).toString();  
            System.out.println(key + " " + value);  
        } 
    }
    public static Map<String,Boolean> splitString(String reply){
        Map<String, Boolean> mapping = new HashMap<>();
        String[] mappings = reply.split(",");
        for(String s : mappings) {
            String[] parts = s.split(":");
            mapping.put(parts[0],Boolean.parseBoolean(parts[1]));
        }
        return mapping;
    }
}
公共类拆分字符串示例{
公共静态void main(字符串[]args){
String reply=“1:true、2:false、3:true、4:false、5:false、6:false”;
映射示例=拆分字符串(回复);
for(字符串名称:example.keySet()){
String key=name.toString();
字符串值=example.get(name).toString();
System.out.println(键+“”+值);
} 
}
公共静态映射拆分字符串(字符串回复){
Map mapping=newhashmap();
字符串[]映射=reply.split(“,”);
for(字符串s:映射){
String[]parts=s.split(“:”);
mapping.put(部分[0],Boolean.parseBoolean(部分[1]);
}
回归映射;
}
}

对于map对象,您可以使用
mapObject.get()
访问相应的布尔值。在您的情况下,
mapObject.get(“1”)
将返回
true
mapObject.get(“2”)false
,依此类推,您可以通过正则表达式实现:

//Compile the regular expression patern
Pattern p = Pattern.compile("([0-9]+):(true|false)+?") ; 
//match the patern over your input
Matcher m = p.matcher("1:true,2:false,3:true,4:false,5:false,6:false") ; 

// iterate over results (for exemple add them to a map)
Map<Integer, Boolean> map = new HashMap<>();
while (m.find()) {
    // here m.group(1) contains the digit, and m.group(2) contains the value ("true" or "false")
    map.put(Integer.parseInt(m.group(1)), Boolean.parseBoolean(m.group(2)));
    System.out.println(m.group(2)) ;
}
//编译正则表达式模式
Pattern p=Pattern.compile(“([0-9]+):(真|假)+?”;
//根据您的输入匹配模式
Matcher m=p.Matcher(“1:true,2:false,3:true,4:false,5:false,6:false”);
//迭代结果(例如,将结果添加到地图)
Map Map=newhashmap();
while(m.find()){
//这里m.group(1)包含数字,m.group(2)包含值(“真”或“假”)
map.put(Integer.parseInt(m.group(1)),Boolean.parseBoolean(m.group(2));
系统输出println(m.group(2));
}
有关正则表达式语法的更多信息,请参见:


编辑:使用
replaceFirst
将列表更改为映射,并使用regex:
,\\d:
将其拆分,然后将每个值存储在数组中

String str = "1:true,2:false,3:true,4:false,5:false,6:false".replaceFirst("1:","");
String[] strArray =  str.split(",\\d:");
System.out.println(Arrays.toString(strArray));
输出

[true, false, true, false, false, false]

您可以使用
模式
覆盖应用于
字符串的匹配结果
svo.getReplies()
重新编写:


首先在逗号上拆分,然后在冒号上拆分,并将其存储在变量中。您需要将其直接放在变量上,如
firstVal
secondVal
,或可用于其他结构,如数组或映射?感谢@tistorm的帮助。
[true, false, true, false, false, false]
String input = "1:true,2:false,3:true,4:false,5:false,6:false";
String[] result = Pattern.compile("(true|false)")
  .matcher(input)
  .results()
  .map(MatchResult::group)
  .toArray(String[]::new);
 System.out.println(Arrays.toString(result)); // [true, false, true, false, false, false]
 String firstVal = result[0]; // true
 String secondVal = result[1]; // false
 // ...