4

Normally in Python 2/3 we may use the following code to split two space-separated integers into two variables:

a,b = map(int,input().split())

Is there a short C# equivalent to this? (i.e nothing as long as below)

string[] template = Console.ReadLine().Split();
a = Convert.ToInt32(template[0]);
b = Convert.ToInt32(template[1]);
Kookie
  • 328
  • 4
  • 14
  • Add space to `Split(' ')`, to define a split character. And C# was not designed to split values in a way, but now it is probably possible, but I would restrain from this 'simplification' – Julo Sep 26 '18 at 07:14
  • If you have the long version then you can always wrap it in your own method to fit your needs. – jegtugado Sep 26 '18 at 07:16
  • I think you might be looking for a c# equivalent to unpacking an array into multiple variables rather than a `map` equivalent (See the second answer on the duplicate rather than accepted one) – Sayse Sep 26 '18 at 07:19
  • If you want a short version, I wrote a neat helper which can be used for structs. See https://dotnetfiddle.net/clTrKN – jegtugado Sep 26 '18 at 07:49

2 Answers2

5

You could try this:

var result = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

However, the above code would crash if the input is not valid.

A more fault tolerant approach would be the following:

var result = Console.ReadLine()
                    .Split(' ')
                    .Select(input => 
                    {
                        int? output = null;
                        if(int.TryParse(input, out var parsed))
                        {
                            output = parsed;
                        }
                        return output;
                    })
                    .Where(x => x != null)
                    .Select(x=>x.Value)
                    .ToArray();
Christos
  • 53,228
  • 8
  • 76
  • 108
  • You could simplify your lambda to `s => int.TryParse(s, out var i) ? i : default` – Emaro Sep 26 '18 at 07:28
  • 1
    @Emaro I didn't follow this approach, since I made the assumption that when parse fails, the user has not given a valid integer. So for an input 3 4 a b 10, I would expect result to be an array of three elements 3,4 and 10 and not of five 3,4,0,0 and 10. I think that this approach makes sense, because different inputs should have different outputs and not the same. For instance for the input 3 4 0 0 10 the result would be the same as in the case of input 3 4 a b 10. – Christos Sep 26 '18 at 07:50
  • 1
    Valid point. Of course you still can use the short version, just with another default value. Say `int.TryParse(s, out var i) ? i as int? : null` – Emaro Sep 26 '18 at 07:53
  • @Emaro sure this can be done. – Christos Sep 26 '18 at 07:59
2

It's called Select(). You need to import Linq:

using System.Linq;

Then you can use it similar to map. Be aware that it is an extension function and is not the exact equivalent.

var integers = Console.ReadLine().Split().Select(s => Convert.ToInt32(s)).ToArray();
var a = integers[0];
var b = integers[1];

This example lacks any proper error handling.


Edit

  • Add ToArray()
  • Write out lambda, which is needed due to the overloads of Convert.ToInt32
Emaro
  • 1,397
  • 1
  • 12
  • 21