Java - Split String by Number and Letters

前端 未结 10 1803
春和景丽
春和景丽 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:53

    I did this as following

    ArrayList integerCharacters = new ArrayList();
    ArrayList stringCharacters = new ArrayList<>();
    
    String value = "C3H20IO"; //Your value 
    String[] strSplitted = value.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); //Split numeric and strings
    
    for(int i=0; i

提交回复
热议问题