F
F
f1awe2021-10-21 20:41:51
Java
f1awe, 2021-10-21 20:41:51

How to convert string to multiple numbers?

I have a string like '123, 123, 123'

How can I convert this string into three numbers so that I can pass them to new Color(123, 123, 123) ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-10-21
Hasanly @azerphoenix

Good afternoon.
Use the method split()and split the string using запятую. Get an array of substrings, convert them to the desired format (for example, int) and that's it.

I
Iskak, 2021-10-23
@Iskak9

It is possible through Stream.

var rgb = Stream.of(str.split(", ")).mapToInt(Integer::parseInt).toArray();

and further to Color.
new Color(rgb[0], rgb[1], rgb[2]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question