views:

48

answers:

3

Hi all , I'm a beginner in C# , I'm trying to do this ..... user input "43 24" and the application take this input , put 43 in arr1[0] , and 24 in arr1[1] . The arr1 is char[] type .. i tried this : (This is only a part of the code of course) (wholeLine is type string)

foreach (char ch in wholeLine)
      {
          if (ch != ' ')
              {                     
                 arr1[0]  += ch ;                     
              }
      }

and the output for arr[0] is : g

I tried to make arr1 an int[] type and did this :

foreach (char ch in wholeLine)
      {
          if (ch != ' ')
              {
                 int z = Convert.ToInt32(ch.ToString());
                 arr1[0]  += z ;                     
              }
      }

But the output is : 7

I just want arr[0] to contain 43 , i think it's a conversions issue , but i have no clue what to do , so help please :)

Thanks in advance .

A: 

You could use a string array. If you call .Split on your string, it will return an array of strings:

string words = "This is a list of words, with: a bit of punctuation" +
                   "\tand a tab character.";

string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

foreach (string s in split) {

    if (s.Trim() != "")
        Console.WriteLine(s);
}
Oded
A: 

you can use wholeLine.Split(" ") which will split your string and make string[] array out of it.

eugeneK
Where is the Split(...) that takes a single string, rather than an array (of strings or chars)? Not a slating - I suggested the same and edited my comment shortly afterwards. There really should be a good old plain Split(string delim)!
Will A
and i mistyped because of keyboard, i don't really care anyway. the way it is my answer is wrong anyway even though it was first.
eugeneK
A: 

Try string[] arr1 = wholeLine.Split(new char[] {' '});

Will A