Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string behind consecutive numbers in string?

I'm using Unity and an answer in UnityScript especially or C# would help. I'm making a calculator using eval() and I need exponents using ^

I want to take:

1+2^

and replace ^ with , and insert Mathf.Pow( behind 2.

I need it to work even when "2" has more than one digit.

It needs to become:

1+Mathf.Pow(2, 
//Mathf.Pow is exponents using 2 parameters

Now, when user types 3, it becomes 1+Mathf.Pow(2,3. I already know how to add ) when user types a non-number.

like image 378
ei1 Avatar asked Apr 26 '26 05:04

ei1


1 Answers

Just Replace and Insert is enough.

function Start () 
{    
var s : String = "1+1245^6+3";
var MathString : String = "Mathf.Pow(";
var MathString1 : String = ")";
var strResult : String  = "";
var j : int;
s = s.Replace('^', '.');

for( var i=0; i< s.length; i++)
{
  if(s[i]=='+' || s[i]=='-' || s[i]=='/')   
    j=i;        
  if(s[i]=='.')
  {
     strResult = s.Insert((i+2), MathString1 );
     s= strResult;
     print(j);
     strResult = s.Insert((j+1), MathString );
  } 
 }
 print(strResult);// 1+Mathf.Pow(1245.6)+3
}
like image 126
Rasa Mohamed Avatar answered Apr 27 '26 17:04

Rasa Mohamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!