-2

I'm not sure what I'm doing is even possible, but I've tried a variety of combos and feel it should be possible - I hope so!

I'm trying to split a long string on a comma directly in to a set of variables, e.g.

string A1; string B2; string C3;

string WHOLE = "xxx,yyy,zzz";

Result will be:

A1 = xxx, B2 = yyy and C3 = zzz

I want to split that string WHOLE in to its 3 parts, directly in to A1,B2,C3...

I've tried:

string[A1, A2, C3] = WHOLE.Split(',');

string[] (A1, A2, C3) = WHOLE.Split(',');

string[] {A1, A2, C3} = WHOLE.Split(',');

I hope you get the idea, I'm also currently trying to figure out if enumerators are applicable here but I'm still quite fresh to C# and never used them in VB.Net - I feel like c# will have this functionality I'm just not sure where to begin...

abatishchev
  • 98,240
  • 88
  • 296
  • 433
jamheadart
  • 5,047
  • 4
  • 32
  • 63
  • Why do they have to be in specific variables and not in an array/collection of some sort? You wont know the number of parts your string will split into at runtime so how could you expect to have the appropriate number of variables – maccettura May 01 '18 at 17:25
  • possible duplication: https://stackoverflow.com/questions/1422155/c-sharp-assign-values-of-array-to-separate-variables-in-one-line – Prasad Telkikar May 01 '18 at 17:27
  • 1
    What do you expect to happen if there is more or less than 2 commas? – Servy May 01 '18 at 17:28
  • Servy, Maccettura, there won't be - it's checked before-hand to make sure we've got the correct string as part of a validation process – jamheadart May 01 '18 at 17:32
  • `string test = "xxx,yyy,zzz"; (string a, string b, string c) = (test.Split(',')[0], test.Split(',')[1], test.Split(',')[2]);` This would require lots of 'Split' calls, though... – Sebastian Hofmann May 01 '18 at 17:37
  • @SebastianHofmann really wasteful to split 3 different times... Why not store the result of the first split? – maccettura May 01 '18 at 17:41
  • @maccettura because it wouldn't be a one-liner then... – Sebastian Hofmann May 01 '18 at 17:44

2 Answers2

1

I don't think that is possible using the default string.split method. It returns an array of the parts, and not a tertiary of variables.

Also, remember that strings are immutable in c#. So something like string[A1, A2, C3] = WHOLE.Split(','); just doesn't make sense with the current constructs. (well, to me at least)

The best I can come up with right now is.

int a1=0; int a2=1; int a3=2;
string parts = "xxx, yyy, zzz".split(',');
console.log(parts[a1]);

Is there a problem you are trying to sole that requires this to be a one-liner, and also not just use an indexed array?

  • It's not a massive problem, but I have a bunch of variables (20 or so) I am calling on by name in lots of different places and they get their values from this fairly long string - if I use indexed array then I have to remember the index number for each variable? Which is why I was wondering about enumerables - just not sure about them yet :/ – jamheadart May 01 '18 at 18:05
  • @jamheadart What if the requirements change and you need to add 20 more? Much more simpler to add them to one collection, then update all the places where they appear with 20 more variables. – Jimenemex May 01 '18 at 18:49
  • I know it's far from ideal but I don't see a good way of coding references to a collection of 20+ variables by number. It's easy to say "Function=a" and "Department=b" than "collection[4]=a" and "collection[17]=b" - without looking up my list of collection entries first – jamheadart May 01 '18 at 19:01
  • This totally isn't ideal. Personally, I would need to look deeper into the design and probably make other changes so this problem goes away. –  May 01 '18 at 21:52
0

Is this something you were looking for?

string A1; string B2; string C3;

string WHOLE = "xxx,yyy,zzz";
A1 = WHOLE.Split(',')[0]; // xxx
B2 = WHOLE.Split(',')[1]; // yyy
C3 = WHOLE.Split(',')[2]; // zzz

If you can use some sort of collection, then you can do this:

string WHOLE = "xxx,yyy,zzz";
IEnumerable<string> list;

list = WHOLE.Split(',');

foreach(string l in list)
{
    Console.WriteLine(l); 
}

// xxx
// yyy
// zzz
// ...

You can even simplify it to:

string WHOLE = "xxx,yyy,zzz,...";

foreach(string l in WHOLE.Split(','))
{
    // Do something
    Console.WriteLine(l);
}

You should consider using a list if you don't know the amount of variables you will need, or the amount of things being split. It's much easier to maintain.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56
  • How do you know how many pieces will be in the resulting split? What if the input was `"www,xxx,yyy,zzz"` or `"yyy,zzz"`? – maccettura May 01 '18 at 17:28
  • That gives me the desired result but I don't want to split it so many times, I'm looking for a direct one-liner approach otherwise I've pretty much got it sussed. – jamheadart May 01 '18 at 17:29
  • Well, I'm checking the count of delimiter "," in the string first, because if it isn't the correct amount then we've been sent the wrong string! So I'm also error-checking before attempting to split. – jamheadart May 01 '18 at 17:32
  • This really isn't something that should be done all on one line, it's going to make it very hard to read... But if you want it on one line, you can just remove all the line feeds at the end of the lines... – Taegost May 01 '18 at 17:33
  • OK so maybe not what I mean; One function rather than one line is what I was hoping for. – jamheadart May 01 '18 at 17:39