## 8 replies

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

• dubsdj

Member

52 Points

333 Posts

### String manipulation question

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.

thanks

MCTS, MCITP,CCA-V
• A2H

All-Star

50681 Points

9849 Posts

### Re: String manipulation question

#### 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
• dubsdj

Member

52 Points

333 Posts

### Re: String manipulation question

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
• dubsdj

Member

52 Points

333 Posts

### Re: String manipulation question

``` 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
• A2H

All-Star

50681 Points

9849 Posts

### Re: String manipulation question

#### 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;

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
• dubsdj

Member

52 Points

333 Posts

### Re: String manipulation question

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

MCTS, MCITP,CCA-V
• dubsdj

Member

52 Points

333 Posts

### Re: String manipulation question

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
• A2H

All-Star

50681 Points

9849 Posts

### Re: String manipulation question

#### 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;

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
• dubsdj

Member

52 Points

333 Posts