1

I'm making a graphing calculator in Unity and I have input with strings like "3+10" and I want to split it to "3","+" and "10".

I can figure out a way to deal with them once I've got them to this form, but I really need a way to split the string to the left and right of key characters such as plus, times, exponent, etc.

I'm doing this in Unity, but a way to do this in any language should help.

Bart
  • 19,692
  • 7
  • 68
  • 77
ei1
  • 301
  • 3
  • 4
  • 10

5 Answers5

1

C#

The following code will do what you asked for (and nothing more).

string input = "3+10-5";
string pattern = @"([-+^*\/])";

string[] substrings = Regex.Split(input, pattern);

// results in substrings = {"3", "+", "10", "-", "5"}

By using Regex.Split instead of String.Split you are able to retrieve the math operators as well. This is done by putting the math operators in a capture group ( ). If you're not familiar with regular expressions you should google the basics.

The code above will stubbornly use the math operators to split your string. If the string doesn't make sense, the method doesn't care and may even produce unexpected results. For example "5//10-" will result in {"5", "/", "", "10", "-", ""}. Note that only one / is returned and empty strings are added.

You can use more complex regular expressions to check if your string is a valid mathematical expression before you try to split it. For example ^(\d+(?:.\d+)?+([-+*^\/]\g<1>)?)$ would check if your string consists of a decimal number and zero or more combinations of an operator and another decimal number.

Stefan Hoffmann
  • 3,214
  • 16
  • 30
0

Here is the C# way -- which I mention because you are using Unity.

words = phrase.Split(default(string[]),StringSplitOptions.RemoveEmptyEntries);

https://msdn.microsoft.com/en-us/library/tabh47cf%28v=vs.110%29.aspx

B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
0

R: EDITED

Take your input vector as x<-c("3+10", "4/12" , "8-3" ,"12*1","1+2-3*4/8").

We can use the following string split based on regex:

> strsplit(x,split="(?<=\\d)(?=[+*-/])|(?<=[+*-/])(?=\\d)",perl=T)
[[1]]
[1] "3"  "+"  "10"

[[2]]
[1] "4"  "/"  "12"

[[3]]
[1] "8" "-" "3"

[[4]]
[1] "12" "*"  "1" 

[[5]]
[1] "1" "+" "2" "-" "3" "*" "4" "/" "8"

How it works:

Split the string when one of two things is found:

  • A digit followed by an arithmetic operator. (?<=\\d) finds something immediately preceded by a digit, while (?=[+*-/]) finds something immediately succeeded by an arithmetic operator, i.e. +, *, -, or /. The "something" in both cases is the blank string "" found between a digit and an operator, and the string is split at such a point.
  • An arithmetic operator followed by a digit. This is just the reverse of the above.
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
0

Here is Java code for splitting a String by math operators

String[] splitByOperators(String input) {
   String[] output = new String[input.length()];
    int index = 0;
    String current = "";
    for (char c : input){
        if (c == '+' || c == '-' || c == '*' || c == '/'){
            output[index] = current;
            index++;
            output[index] = c;
            index++;
            current = "";
        } else {
             current = current + c;
        }
    }
    output[index] = current;
    return output;
}
Jaboyc
  • 567
  • 1
  • 8
  • 16
0

Using Python regular expressions:

>>> import re
>>> match = re.search(r'(\d+)(.*)(\d+)', "3+1")
>>> match.group(1)
'3'
>>> match.group(2)
'+'
>>> match.group(3)
'1'

The reason for using regular expressions is for greater flexibility in handling a variety of simple arithmetic expressions.