Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for String.split() to return tuple?

Tags:

c#

I have string which I need to split() and assign it's value into two variables. Is it possible for split() to return tuple instead of string[] so I can do something like:

String myString = "hello=world" value, id = myString.Split('='); 

I'm looking for some elegant solution.

like image 798
Kajbo Avatar asked Mar 09 '18 09:03

Kajbo


People also ask

Does split return a list or tuple?

split() returns a list while str. partition() returns a tuple . This is significant since a list is mutable while a tuple is not.

How do you split a string into a tuple?

When it is required to convert a string into a tuple, the 'map' method, the 'tuple' method, the 'int' method, and the 'split' method can be used. The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.

Does split in Python return a list?

The split() method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype.

How do you split a string into two variables in Python?

To split string variables at each whitespace, we can use the split() function with no arguments. The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split.


Video Answer


1 Answers

If you can use C# 7 - you can use tuple deconstruction. If type has static or extension method named Deconstruct with appropriate signature - this type can be deconstructed. So you can have extension method like this:

public static class Extensions {     public static void Deconstruct<T>(this IList<T> list, out T first, out IList<T> rest) {          first = list.Count > 0 ? list[0] : default(T); // or throw         rest = list.Skip(1).ToList();     }      public static void Deconstruct<T>(this IList<T> list, out T first, out T second, out IList<T> rest) {         first = list.Count > 0 ? list[0] : default(T); // or throw         second = list.Count > 1 ? list[1] : default(T); // or throw         rest = list.Skip(2).ToList();     } } 

And then you can deconstruct string array (which implements IList<string>) with this syntax (you might need to add appropriate using so that extension method above is reachable):

var line = "a=b"; var (first, second, _) = line.Split('='); Console.WriteLine(first); // "a" Console.WriteLine(second); // "b" 

or

var line = "a=b"; var (first, (second, _)) = line.Split('='); Console.WriteLine(first); // "a" Console.WriteLine(second); // "b" 

Which is quite close to what you need.

With just first extension method above (which deconstructs to first element and the rest) you can deconstruct to arbitrary length:

var (first, (second, _)) = line.Split('='); var (first, (second, (third,  _))) = line.Split('='); var (first, rest) = line.Split('='); // etc 

Second extension method is needed only if you want a little bit more convenient syntax for deconstruction of first 2 values (var (first, second, rest) instead of var (first, (second, rest)))

like image 143
Evk Avatar answered Sep 19 '22 21:09

Evk