splitting a string and then converting

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 3
Reputation: FilteR is an unknown quantity at this point 
Solved Threads: 0
FilteR FilteR is offline Offline
Newbie Poster

splitting a string and then converting

 
0
  #1
Aug 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 10
Reputation: CodeMonkey775 is an unknown quantity at this point 
Solved Threads: 0
CodeMonkey775 CodeMonkey775 is offline Offline
Newbie Poster

Re: splitting a string and then converting

 
0
  #2
Aug 1st, 2007
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.

  1. string expression = "12 + 3";
  2. char[] separators = new char[] { Convert.ToChar("+"), Convert.ToChar("-"), Convert.ToChar("*"), Convert.ToChar("/") };
  3.  
  4. foreach (string number in expression.Split(separators))
  5. {
  6. int convert = Convert.ToInt32(number));
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: tostrinj is an unknown quantity at this point 
Solved Threads: 1
tostrinj's Avatar
tostrinj tostrinj is offline Offline
Newbie Poster

Re: splitting a string and then converting

 
0
  #3
Aug 1st, 2007
How about this:

  1. string g = "12 + 3";
  2. string[] MyInts = g.Split('+');
  3. ArrayList RealInts = new ArrayList();
  4.  
  5. for(int i = 0; i < MyInts.Lenght; i++){
  6. RealInts.Add(Convert.ToInt32(((string)MyInts[i]).Trim());
  7. }


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

  1. string g = "12 + 3";
  2.  
  3. char [] MyInts = g.ToCharArray();
  4. ArrayList RealInts = new ArrayList();
  5.  
  6. Foreach(char c in MyInts){
  7. if(IsNumeric(c.ToString())){
  8. RealInts.Add(Convert.ToInt32(c));
  9. }
  10. }

After this you should get something like this:

RealInts[0] = 1;
RealInts[1] = 2;
RealInts[2] = 3;
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4
Reputation: subburaj.r is an unknown quantity at this point 
Solved Threads: 0
subburaj.r subburaj.r is offline Offline
Newbie Poster

Re: splitting a string and then converting

 
0
  #4
Aug 2nd, 2007
Originally Posted by FilteR View Post
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
C#
====
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]);



Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC