How to pick up only integer data from an ArrayList of String

前端 未结 4 1571
青春惊慌失措
青春惊慌失措 2021-01-29 11:48

I read a text file and put the data into an ArrayList of String.

The data is like

China 12351235123 Korea 123532523 USA 1234123         


        
4条回答
  •  旧巷少年郎
    2021-01-29 12:30

    I am assuming that your ArrayList is of type String. replaceAll() will take out anything that isn't a number(0-9)

    ArrayList countrys = new ArrayList();
    countrys.add("China 12351235123");
    countrys.add("Korea 123532523");
    countrys.add("USA 12341235123");
    
    for(String c:countrys){
        String number = c.replaceAll("[^0-9]", "");
        System.out.println(number);
    }
    

提交回复
热议问题