| | |
splitting a string and then converting
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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
Views: 3276 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array bitmap box broadcast buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ http httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update validation visualstudio webbrowser windows winforms wpf xml





