I want to parse latex string to math expression with following code
static string ParseString(List<string> startItems, string input)
{
foreach (var item in startItems)
{
int charLocation = input.IndexOf(item, StringComparison.Ordinal);
if (charLocation > 0)
{
switch (input.Substring(0, charLocation).Last())
{
case '{':
case '+':
case '-':
case '*':
break;
default:
input = input.Replace(item, "*" + item);
break;
}
}
}
return input;
}
static void Main(string[] args)
{
string ToParse = @"\sqrt{3}\sqrt{3}*4\sqrt{3}";
ToParse = ParseString(new System.Collections.Generic.List<string> { @"\frac", @"\sqrt" }, ToParse);
Console.WriteLine(ToParse);
}
I should get before every \sqrt word multiply char.But my program breaks in first \sqrt and cant parse others. Result is
\sqrt{3}\sqrt{3}*4\sqrt{3}
And i want to get
\sqrt{3}*\sqrt{3}*4*\sqrt{3}
Sorry for bad English.