944,141 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4789
  • C# RSS
Aug 1st, 2007
0

splitting a string and then converting

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FilteR is offline Offline
3 posts
since Jul 2007
Aug 1st, 2007
0

Re: splitting a string and then converting

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)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CodeMonkey775 is offline Offline
10 posts
since Jul 2007
Aug 1st, 2007
0

Re: splitting a string and then converting

How about this:

C# Syntax (Toggle Plain Text)
  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

C# Syntax (Toggle Plain Text)
  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;
Reputation Points: 12
Solved Threads: 1
Newbie Poster
tostrinj is offline Offline
20 posts
since Jul 2007
Aug 2nd, 2007
0

Re: splitting a string and then converting

Click to Expand / Collapse  Quote originally posted by FilteR ...
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]);



Reputation Points: 10
Solved Threads: 0
Newbie Poster
subburaj.r is offline Offline
4 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Remoting - Code not executing after an Activator object call
Next Thread in C# Forum Timeline: Creating DataGrid for Oracle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC