How to get the separate digits of an int number?

前端 未结 30 2579
陌清茗
陌清茗 2020-11-22 03:03

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0.

How can I get

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 03:24

    if digit is meant to be a Character

    String numstr = Integer.toString( 123 );
    Pattern.compile( "" ).splitAsStream( numstr ).map(
      s -> s.charAt( 0 ) ).toArray( Character[]::new );  // [1, 2, 3]
    

    and the following works correctly
    numstr = "000123" gets [0, 0, 0, 1, 2, 3]
    numstr = "-123"    gets [-, 1, 2, 3]

提交回复
热议问题