开发者

Adding two Integers to array index without summing them

开发者 https://www.devze.com 2023-01-06 12:39 出处:网络
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

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 .


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);
}


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消