| | |
splitting a string and then converting
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 10
Reputation:
Solved Threads: 0
I'm not really sure what you are planning to do with it once you convert it, but this is how you would get the numbers out as integers. To actually evaluate the expression within C#, you would have to delve into CodeDom where you compile C# code during runtime.
C# Syntax (Toggle Plain Text)
string expression = "12 + 3"; char[] separators = new char[] { Convert.ToChar("+"), Convert.ToChar("-"), Convert.ToChar("*"), Convert.ToChar("/") }; foreach (string number in expression.Split(separators)) { int convert = Convert.ToInt32(number)); }
How about this:
At the end you should get an array list of integers.
RealInts[0] = 12;
RealInts[1] = 3;
If you want individual chars converted into ints then something like this should do it
After this you should get something like this:
RealInts[0] = 1;
RealInts[1] = 2;
RealInts[2] = 3;
C# Syntax (Toggle Plain Text)
string g = "12 + 3"; string[] MyInts = g.Split('+'); ArrayList RealInts = new ArrayList(); for(int i = 0; i < MyInts.Lenght; i++){ RealInts.Add(Convert.ToInt32(((string)MyInts[i]).Trim()); }
At the end you should get an array list of integers.
RealInts[0] = 12;
RealInts[1] = 3;
If you want individual chars converted into ints then something like this should do it
C# Syntax (Toggle Plain Text)
string g = "12 + 3"; char [] MyInts = g.ToCharArray(); ArrayList RealInts = new ArrayList(); Foreach(char c in MyInts){ if(IsNumeric(c.ToString())){ RealInts.Add(Convert.ToInt32(c)); } }
After this you should get something like this:
RealInts[0] = 1;
RealInts[1] = 2;
RealInts[2] = 3;
•
•
Join Date: May 2007
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
When i type in a string consisting of numbers and signs for example:
12 + 3
how can i split it and how can i convert parts of the string into integers?
been searching in the web for 2 hours didnt find anything apropreate
====
string str_string = "12+3";
string[] str_arrstring = str_string.Split('+');
int s_first = Convert.ToInt32(str_arrstring[0]);
int s_second = Convert.ToInt32(str_arrstring[1]);
![]() |
Similar Threads
- Splitting a string? (C)
- splitting a string to many integers (C++)
- Converting String to Integer help (C++)
Other Threads in the C# Forum
- Previous Thread: Remoting - Code not executing after an Activator object call
- Next Thread: Creating DataGrid for Oracle
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





