 ## 8 replies

Last post Nov 09, 2017 06:49 PM by dubsdj

• ### String manipulation question

Nov 08, 2017 04:42 PM|dubsdj|LINK

Hi there

I am developing an AI and wanted to see if anyone could give me some idea of how I would go about this?

the person types: what is 1 + 1 ?

my question is, how could I go about manipulating the string to detect the + sign and then get the numbers either side of that, which could be any amount of numbers each side ?

so it could be : what is 1500 + 2500

I guess I would then somehow need to convert the numbers in the string to integers or floats and perform the calculation?

I have used indexOF before but I think it might be a bit more tricky than that.

Any advice would be appreciated.

thanks

MCTS, MCITP,CCA-V
• ### Re: String manipulation question

Nov 08, 2017 05:13 PM|A2H|LINK

#### dubsdj

what is 1500 + 2500

if the input value is always of this format, Then you can use below code to get the values before and after plus(+) symbol

```int afterValue = 0;
int BeforeValue = 0;
string test = "what is 1500 + 2500";

//Code to get value before + sign
int plusPositionIndex = test.IndexOf("+");
BeforeValue = int.Parse(System.Text.RegularExpressions.Regex.Match(test.Substring(0, plusPositionIndex), @"\d+").Value);
//Code to get value after + sign
int plusPositionLastIndex = test.LastIndexOf("+");
int adjustedPosA = plusPositionLastIndex + 1;
Thanks,
Aje
My Blog | Dotnet Funda
• ### Re: String manipulation question

Nov 08, 2017 05:17 PM|dubsdj|LINK

I was trying something like this but it only does addition and doesn't recognise decimal points:

```protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int result = doMath("what is 30 + 10 ?");
}
}

public int doMath(String text)
{

int result = 0;

String numberStr = String.Empty;
for ( int i = 0; i < text.Length; i++)
{
char c = text[i];

if (c >= '0' && c <= '9' )
{
numberStr += c;
}

else if ( !string.IsNullOrWhiteSpace(numberStr))
{
result += int.Parse(numberStr);
numberStr = String.Empty;

}

}

return result;

}```

MCTS, MCITP,CCA-V
• ### Re: String manipulation question

Nov 08, 2017 05:25 PM|dubsdj|LINK

I tried your method:

``` public int doMath2(String text)
{
int result = 0;
int afterValue = 0;
int BeforeValue = 0;
//Code to get value before + sign
int plusPositionIndex = text.IndexOf("+");
BeforeValue = int.Parse(Regex.Match(text.Substring(0, plusPositionIndex), @"\d+").Value);
//Code to get value after + sign
int plusPositionLastIndex = text.LastIndexOf("+");
int adjustedPosA = plusPositionLastIndex + 1;

result = BeforeValue + afterValue;

return result;
}
```

but it says:

## Input string was not in a correct format.

MCTS, MCITP,CCA-V
• ### Re: String manipulation question

Nov 08, 2017 05:46 PM|A2H|LINK

#### dubsdj

Input string was not in a correct format.

There is a question mark symbol at the last which was not there in your initial post. That was causing the issue. You can try with the below code which handle the question mark at last scenario and also do the basic 4 mathematical operations. It also handles the decimal point as well

``` public double doMath(String text)
{
//return result;
double result = 0;
double afterValue = 0;
double BeforeValue = 0;

//Get the operator here
string MathOperator = System.Text.RegularExpressions.Regex.Match(text, @"[*+/-]").Value;

//Code to get value before special sign
int plusPositionIndex = text.IndexOf(MathOperator);
BeforeValue = int.Parse(System.Text.RegularExpressions.Regex.Match(text.Substring(0, plusPositionIndex), @"\d+").Value);
//Code to get value after special sign
int plusPositionLastIndex = text.LastIndexOf(MathOperator);
int adjustedPosA = plusPositionLastIndex + 1;
afterValue = int.Parse(System.Text.RegularExpressions.Regex.Match(text.Substring(adjustedPosA), @"\d+").Value);

switch (MathOperator)
{
case "+":
result = BeforeValue + afterValue;
break;
case "-":
result = BeforeValue - afterValue;
break;
case "/":
result = BeforeValue / afterValue;
break;
case "*":
result = BeforeValue * afterValue;
break;
default:
result = BeforeValue + afterValue;
break;
}
return result;
}```

Thanks,
Aje
My Blog | Dotnet Funda
• ### Re: String manipulation question

Nov 08, 2017 07:15 PM|dubsdj|LINK

genius you are! I will give that a go thank you.

MCTS, MCITP,CCA-V
• ### Re: String manipulation question

Nov 09, 2017 12:53 PM|dubsdj|LINK

so I tried asking it: what is 10.5 + 10.5 ?

it says 20

which I guess means it didn't take the decimal point into account.

MCTS, MCITP,CCA-V
• ### Re: String manipulation question

Nov 09, 2017 02:26 PM|A2H|LINK

#### dubsdj

what is 10.5 + 10.5 ?

it says 20

which I guess means it didn't take the decimal point into account.

Yes there as an issue in code. I Thought of handling decimal points in result value not with the input values. Sorry for this mistake.

You can try with the below updated code

```  public double doMath(String text)
{
//return result;
double result = 0;
double afterValue = 0;
double BeforeValue = 0;

//Get the operator here
string MathOperator = System.Text.RegularExpressions.Regex.Match(text, @"[*+/-]").Value;

//Code to get value before special sign
int plusPositionIndex = text.IndexOf(MathOperator);
BeforeValue = double.Parse(System.Text.RegularExpressions.Regex.Match(text.Substring(0, plusPositionIndex), @"[\d]{1,10}([.][\d]{1,20})?").Value, System.Globalization.CultureInfo.InvariantCulture);
//Code to get value after special sign
int plusPositionLastIndex = text.LastIndexOf(MathOperator);
int adjustedPosA = plusPositionLastIndex + 1;
afterValue = double.Parse(System.Text.RegularExpressions.Regex.Match(text.Substring(adjustedPosA), @"[\d]{1,10}([.][\d]{1,20})?").Value, System.Globalization.CultureInfo.InvariantCulture);

switch (MathOperator)
{
case "+":
result = BeforeValue + afterValue;
break;
case "-":
result = BeforeValue - afterValue;
break;
case "/":
result = BeforeValue / afterValue;
break;
case "*":
result = BeforeValue * afterValue;
break;
default:
result = BeforeValue + afterValue;
break;
}
return result;
}```

Thanks,
Aje
My Blog | Dotnet Funda
• ### Re: String manipulation question

Nov 09, 2017 06:49 PM|dubsdj|LINK

Thanks this is really clever! I'm reading through it trying to make sense of how you perform the calculations.

what if I typed:

what is 10 + 20 + 10

I think it would ignore the last number right? so the answer would be 30?

MCTS, MCITP,CCA-V