1

I have something like this:

  string[] names= {"john","joe", "jim"};
  data="john,4,3,6,joe,3,6,2,jim,3,6,7";
  string[] results=data.Split(names,StringSplitOptions.RemoveEmptyEntries);

this gives:

 ,4,3,6

 ,3,6,2

 ,3,6,7

but i want the names to be in the results array as well.

2 Answers 2

6

How about adding this line at the end:

results = results.Select((x, i) => names[i] + x).ToArray();

This will prepend the name in front of each entry, outputting:

john,4,3,6
joe,3,6,2
jim,3,6,7

Sign up to request clarification or add additional context in comments.

Comments

3

You could keep your original code, then zip in the names:

string[] names= new [] {"john","joe", "jim" };
string data="john,4,3,6,joe,3,6,2,jim,3,6,7";
string[] results = data.Split(names, StringSplitOptions.RemoveEmptyEntries)
                       .Zip(names, (values, name) => name + values)
                       .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.