有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java字符串拆分并获取一些数据

String s = "Total Count :2, Correct Count :1, Wrong Count:1, Correct :India is great,, Wrong: where aer you";

我想要的是:

  • 总数
  • 正确计数
  • 数错了

int变量中

这对我来说相当新鲜,我希望你们能帮助我


共 (1) 个答案

  1. # 1 楼答案

        String s = "Total Count :2, Correct Count :1,Wrong Count:1, Correct :India is great,, Wrong: where aer you";
    
        String[] ints = s.split("[^0-9]+");
    
        int totalCount = Integer.parseInt(ints[1]);
        int correctCount = Integer.parseInt(ints[2]);
        int wrongCount = Integer.parseInt(ints[3]);
    
        System.out.println(totalCount + " " + correctCount + " " + wrongCount); //2 1 1