TheLittleCoder
TheLittleCoder

Reputation: 21

How to separate string into 3 seperate strings with unknown lengths

I am making a calculator program and want it to be able to use fractions like so: 7/8 * 3/4 or 11/12 - 1/10.

I separate the 3 parts firstValue, operation, secondValue.

Now I need to figure out how to separate the fraction into 3 parts without spaces and the values in the fraction has an unexpected amount of digits like 3 and 30.

If it was a fraction like 3/4, I know I could have just separated it by splitting it into all of it's 3 symbols but it could be a fraction like 3/11 so just keeping the separation as a 3 won't work.

I also know I can't use StringTokenizer and separate using "/" or "". Is there any way to do it?

Upvotes: 0

Views: 76

Answers (1)

Bohemian
Bohemian

Reputation: 425238

Simply split on word boundaries:

String[] parts = str.split("\\b");

This splits between word chars and non-word (and visa versa). Digits are considered "word chars".

This will take a String such as "123/456" and return an array ["123", "/", "456"].

Upvotes: 1

Related Questions