0

Below is my string array :

 public string[] categories { get; set; }

Now this categories contains records like below :

 "categories": [
    "electronic,sports",
    "abc,pqr",
    "xyz",
  ]

Input:

string[] categories = { "electronic,sports", "abc,pqr", "xyz"};

Now I want to split values in categories and create records like this but in categories variable only :

So final categories variable should contain output like below:

"categories": [
    "electronic",
    "sports",
    "abc",
    "pqr",
    "xyz",
  ]

So I want my loop to run 5 times; if I loop on to categories variable and further lots of operation is done on this variable only so I don't want to take above final output in other variable.

 foreach (var category in categories)
 {
     //code
 }
3
  • What is your input? is that a part of JSON or what else? Commented Dec 29, 2016 at 6:48
  • 3
    categories = categories.SelectMany(o => o.Split(',')).ToArray(); ? Commented Dec 29, 2016 at 6:49
  • @un-lucky:Updated my question to include sample input Commented Dec 29, 2016 at 6:53

4 Answers 4

7

You can use LINQ SelectMany() and then project the result to array :

using System.Linq;


string[] categories = { "electronic,sports", "abc,pqr", "xyz"};
categories = categories.SelectMany(o => o.Split(',')).ToArray();

foreach(var c in categories)
{
    Console.WriteLine(c);
}

demo

output :

electronic
sports
abc
pqr
xyz
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this worked.thank you so much for the kind help and please keep helping like this.Thank you once again :)
2

You can try without if code;

        List<string> temps = new List<string>();
        foreach (var category in categories)
        {
            temps.AddRange(category.Split(',').ToList());
        }
        categories = temps.ToArray();

1 Comment

Thank you so much for the help and please keep helping like this :)
1

This might do the trick for you

List<string> newcategories = new List<string>();
foreach(var category in categories)
{
    if(category.Contains(","))
    {
        string[] c = category.Split(',');
        newcategories.Add(c[0]);
        newcategories.Add(c[1]);
    }
    else
    {
        newcategories.Add(category);
    }
}

1 Comment

Sorry as i said in my question that i dont want to use new variable.i want in my categories string array only
1

Without linq...just using a couple foreach loops.

        string[] categories = new string[] { "electronic,sports", "abc,pqr", "xyz" };

        foreach(var category in categories)
        {
            foreach(var item in category.Split(','))
            {
                Console.WriteLine(item);
            }
        }

1 Comment

Thank you so much for the help and please keep helping like this :)

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.