3 Replies - 1086 Views - Last Post: 16 September 2013 - 07:51 AM Rate Topic: -----

#1 M.Paaske   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-January 10

string split Issues, split into 2 var's

Posted 16 September 2013 - 07:18 AM

I'm trying to make a calculator in an C# ConsoleApplication, but I've run into an issue.
I'm trying to split a string into 2 var's. like so..
string cal = "2+3";
//int num1;
//int num2;
//int sum = 0;

string[] split = cal.Split('+');

string part1 = " ";
string part2 = " ";

foreach (string value in split)
{
    part1 = cal[0].ToString();
    part2 = cal[2].ToString();
}

/*
            num1 = Convert.ToInt32(part1);
            num2 = Convert.ToInt32(part2);

            Calculate Calcu = new Calculate();

            if (cal.Contains("+"))
                sum = Calcu.add(num1, num2);
            if (cal.Contains("-"))
                sum = Calcu.sub(num1, num2);
            if (cal.Contains("*"))
                sum = Calcu.mul(num1, num2);
            if (cal.Contains("/"))
                sum = Calcu.div(num1, num2);
            */
            Console.WriteLine(part1+part2);
            Console.ReadLine();



This prutty much works as intented, it returns 23 wich is what I wonna add when I uncomment the commented part of my code, the issue apears when I try to add f.ex 2+30, it will still output 23 and not take the 0 into count, can anyone please clearify to me why that is??

I've all so tryed to modyfie the Split like this.
string[] split = cal.Split(new char[] {'+'}); 

Still got the same issue..

I've tryed this aswell with the foreach
foreach (string value in split)
            {
                part1 = value[0].ToString();
                part2 = value[2].ToString();
            }


Wich crashes the program with a fatel error

Quote

Unhanddled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.String.get_Chars<Int32 index>
at calculater.Program.Main<String[] args> in c:\Vusal Studio 2012\Projects\calculater\calculater\Program.cs:line 61


Your help will be most appreciated..

This post has been edited by andrewsw: 16 September 2013 - 07:21 AM
Reason for edit:: Fixed quote tags


Is This A Good Question/Topic? 0
  • +

Replies To: string split Issues, split into 2 var's

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,255
  • Joined: 12-December 12

Re: string split Issues, split into 2 var's

Posted 16 September 2013 - 07:25 AM

There is a simple Split example here at dotnetperls.

foreach (string value in split)

You are looping through the strings but are not doing anything with them (the value). As there are only two strings you don't really need this loop:

string part1 = split[0];
string part2 = split[1];

This post has been edited by andrewsw: 16 September 2013 - 07:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 M.Paaske   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-January 10

Re: string split Issues, split into 2 var's

Posted 16 September 2013 - 07:45 AM

Ohh my god, thanks alot Sir, this has annoyed me for 4 days now.. This actualy fixed my problem completly..

This post has been edited by andrewsw: 16 September 2013 - 07:51 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,255
  • Joined: 12-December 12

Re: string split Issues, split into 2 var's

Posted 16 September 2013 - 07:51 AM

You are quite welcome. Please take the time to go through that dotnetperls page as well ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1