Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string of only ten characters e.g."12345*45688" into an array

I'm making a simple calculator where you type values into an edit box. I need to split the string into a number of arrays depending on how many *+-/ there are in the sum for instance

I have 22+22*22-22/22 I want to break that into five different arrays because there are five different groups of numbers. Then later I am going to add array1 to array two multiply that by array3 and subtract that by array4 and divide that by array 5.

like image 774
user744061 Avatar asked May 08 '11 17:05

user744061


2 Answers

If you want to read something like that, especially if you want to evaluate mathematical expressions, you need more than just an array-splitter; you need a real parser. Doing it right requires a bit of compiler theory. I'd recommend you take a look at Let's Build A Compiler, a tutorial that covers everything you'll need to know about expression parsing (and a bit more, since he's actually building a simple compiler) and makes it easy to understand. All examples are in Turbo Pascal, so it should be easy for a Delphi coder to read.

like image 156
Mason Wheeler Avatar answered Nov 15 '22 03:11

Mason Wheeler


Delphi XE has a SplitString function that does exactly what you need.

like image 32
Uwe Raabe Avatar answered Nov 15 '22 04:11

Uwe Raabe