How to pass multiple delimiters to a substring (Kotlin/Android)?

Viewed 583

I'm trying to make a calculator app, and I need to be able to get the last value after someone presses an operator (+,*,-,/) after the second time (the first value is saved with no issues since it's the only number on the screen). So if the top of the screen has something like (222 * 3333 / 12), I need to get 12. Once I learn how to do this, I can figure out how to save the previous number/sum, then do calculations on the next number (I haven't made that part yet though). I know this is a janky way of doing things, and any suggestions are appreciated. So I can grab this number if I use substringAfterLast() and insert an operator there, however, if mix things up and use multiple operators (like 222 * 3333 / 12), my y variable (see below) just shows "222 * 3333 / 12" instead of 12. How can I use multiple delimiters for substring?

Here's my code by the way (forgive me)

        multiplybutton.setOnClickListener {
            var x = numbersEntered.toString()
            var y = ""
            //creates an array that holds the operators so it can be easily filtered out
            val operators = arrayOf<Char>('*','/','+','-')
            //prevents app from crashing by saving the last value before the operator is added, allowing us to create infinite number of operations
            if (x.any(operators :: contains)){
//                x.split(operators.toString())
                
                y = x.substringAfterLast('*')  // can't use my operator character array here? why? how do I iterate through it?
                Toast.makeText(this, y.toString(), Toast.LENGTH_SHORT).show()

//                previousvalue = y.toInt()
            } else {
                previousvalue = x.toInt()
            }
            numbersEntered.append("*")
            numbersEnteredBox.setText(numbersEntered.toString())
            isMultiply = true;
            Toast.makeText(this, previousvalue.toString(), Toast.LENGTH_SHORT).show()
        }

edit: ignore italics plz, not sure what went wrong

2 Answers

sorry, maybe I not fully understand what you want to do but you can use split function to get last number from string like this 222 * 3333 / 12:

println("222 * 3333 / 12".split('+', '-', '*', '/').last().trim().toInt())
// prints 12

Use split with a regular expression

Using a regular expression gives you a great deal of control over how the string is split. In your case, anything that isn't a numeric digit is probably a delimiter. Regex has a built-in character class, denoted by \D, that will match any character that isn't a digit.

val pattern = Regex("""\D+""") // matches one or more non-digit characters
"1 * 2 / 3".split(pattern).last() // returns "3"

Equally, you could create your own character class, using [], to be more specific about which characters you want to use as delimiters:

val pattern = Regex("""\s*[-\+*]\s*""") // matches -\+*, and any spaces around them
"1 * 2 / 3".split(pattern).last() // returns "3"
Related