2

I can do this using an if statement but I feel like the code is too long for this simple operation.

num = -12345

-->inserts magical code<--

desired output

num_list = ['-1', '2', '3', '4', '5']

  • I have been coding for 20 years, I did need to convert a number into a string hundreds of time. I did need to convert a string into an array of characters hundreds of time. I never needed to convert into an array of characters where the fist character would also contain the sign. it is not a common operation at all. So I doubt you'll fine a library for this. You'll have to write your "if" !! What language is it? – Pascal Ganaye Jul 26 '20 at 22:01
  • python, its for question on a quiz – Adam Zawitkowski Jul 27 '20 at 03:49

1 Answers1

1

You can simply use .split("\\B")

So the magical code would be:

String[] num_list = Integer.toString(num).split("\\B");
Juliano Costa
  • 2,175
  • 2
  • 18
  • 30