Hello all, I was wondering on how I would extract a number out of a String after a specified character. For example, if String s = "22+5+35" , how can I extract and store the numbers 22,5, and 35 in a ArrayList?

Recommended Answers

Convert string to char array, then check each character to see if it is a digit using Character.isDigit(char c)

See isDigit Documentation

OR

Use regex, with the split function.

See Java …

Jump to Post

All 3 Replies

if you have to take into account other actions (+, -, :? *) and or brackets, split might not be the easiest way to go.

If you want to do a split (and the purpose is to obtain only digit characters), use regular expression that splits at any non-digit character (i.e. "[^\d]+"). Then you would get an array containing only digit characters.

If you are dealing with mathematic expressions, spliting a string into a char array would be OK because you will need to deal with the whole string (numbers & operands).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.