Find First Parentheses in String
Hey guys, I'm currently working in an "in game" programming game. What i need is find text in the first parenthese the script found. Here's an example: "((990 / 71) - 3^4.5) + 2 * (36.1 / 44.8 - (76.93 + 9.5))" should be returning this string "((990 / 71) - 3^4.5)". Tell my if you don't understand, i speak french. I have tried many possibility and here's what i found which can be useful: First, find the first index of "(" after find the first index of ")". If there's an other "(" between "(" and ")", like this: "(exemple()", the code need to find the next index of ")", like this "(exemple()example)". I know this the worst question...
Better to look in non-Unity sources. This is a well-known (but tricky) topic, and any answers you find will work just fine inside Unity.
Try searches about representing expressions. I'd guess finding matching parens is also common, but won't be good enough for what you need.
Answer by Briksins · May 05, 2016 at 11:44 PM
Hi, that should help:
string expression = "((990 / 71) - 3^4.5) + 2 * (36.1 / 44.8 - (76.93 + 9.5))";
int match = 0;
var finalExpression = new StringBuilder();
foreach (var character in expression.ToCharArray(0, expression.Length))
{
if (character == '(')
match++;
if (character == ')')
match--;
finalExpression.Append(character);
if (match == 0)
break;
}
Debug.log(finalExpression.ToString());
Ohhh... I just realize how this script work. I will try to replicate without StringBuild. Thanks again!
Answer by FortisVenaliter · May 05, 2016 at 09:18 PM
You should represent the equations as trees. Check out this StackOverflow question, that has a basic primer for how to think about it. It's for Java, but the terms are pretty universal.
Your answer
Follow this Question
Related Questions
Problem with Contains 0 Answers
Cannot convert 'String' to 'Array'. 2 Answers
Use transform calculating string to boolean 1 Answer
Pick a random string from a string array C# 1 Answer
use the tag of a gameobject to assign a string variable 1 Answer