Java - Split String by Number and Letters

前端 未结 10 1798
春和景丽
春和景丽 2020-12-05 20:14

So I have, for example, a string such as this C3H20IO

What I wanna do is split this string so I get the following:

Array1 = {C,H,I,O}
Ar         


        
10条回答
  •  悲&欢浪女
    2020-12-05 20:36

    You could try this approach:

    String formula = "C3H20IO";
    
    //insert "1" in atom-atom boundry 
    formula = formula.replaceAll("(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=\\D)$", "1");
    
    //split at letter-digit or digit-letter boundry
    String regex = "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)";
    String[] atoms = formula.split(regex);
    

    Output:

    atoms: [C, 3, H, 20, I, 1, O, 1]

    Now all even even indices (0, 2, 4...) are atoms and odd ones are the associated number:

    String[] a = new String[ atoms.length/2 ];
    int[] n = new int[ atoms.length/2 ];
    
    for(int i = 0 ; i < a.length ; i++) {
        a[i] = atoms[i*2];
        n[i] = Integer.parseInt(atoms[i*2+1]);
    }
    

    Output:

    a: [C, H, I, O]
    n: [3, 20, 1, 1]

提交回复
热议问题