J
J
Joseph Goodman2018-09-06 23:30:32
Java
Joseph Goodman, 2018-09-06 23:30:32

How to get all numbers from a string?

Good day.
Let's assume that we are given a certain string in which different numbers are listed to us separated by a space. How can we extract all these numbers from a string and write them into an int array?? The following are examples of lines

String numbers = "6 8 15 7 -8";
String numbers1 = "-88 36 15 1 45 90";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivan19631224, 2018-09-07
@lolaevv

A more modern solution with streams and lambdas:

String numbers = "6 8 15 7 -8";
int result[] = Arrays.stream(numbers.split(" +")).mapToInt(Integer::parseInt).toArray();

Although it might be better for a beginner to do as suggested by Andrej Gessel first .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question