0

I need a way to prepend items on to an existing string array, such that:

string[] dirParams = null;
if (Request.Params["locationDirection_0"] != "")
{
 dirParams = Request.Params["locationDirection_0"].Split(',');
}
if (Request.Params["locationDirection_1"] != "")
{
 dirParams = Request.Params["locationDirection_1"].Split(',');
}
if (Request.Params["locationDirection_2"] != "")
{
 dirParams = Request.Params["locationDirection_2"].Split(',');
}
if (Request.Params["locationDirection_3"] != "")
{
 dirParams = Request.Params["locationDirection_3"].Split(',');
}

will give me a string array of about 4 items (assuming none of the requests coming in are empty)

whats the easiest way to do this, I thought of using a list and or a dictonary, neither will work for what I want to do, string array is all I want.

svick
247k54 gold badges407 silver badges536 bronze badges
asked May 25, 2013 at 19:31
2
  • 2
    Why not use a List, then convert to array? Commented May 25, 2013 at 19:33
  • Do you mean prepend? Or do you really mean append? Commented May 25, 2013 at 20:06

4 Answers 4

2

Use a list instead:

 List<string> dirParams = new List<string>();
 if (Request.Params["locationDirection_0"] != "")
 {
 dirParams.AddRange(Request.Params["locationDirection_0"].Split(','));
 }
 if (Request.Params["locationDirection_1"] != "")
 {
 dirParams.AddRange(Request.Params["locationDirection_1"].Split(','));
 }
 if (Request.Params["locationDirection_2"] != "")
 {
 dirParams.AddRange(Request.Params["locationDirection_2"].Split(','));
 }
 if (Request.Params["locationDirection_3"] != "")
 {
 dirParams.AddRange(Request.Params["locationDirection_3"].Split(','));
 }
answered May 25, 2013 at 19:34
Sign up to request clarification or add additional context in comments.

Comments

0

Use a List<string> and then the ToArray() method to convert it into a string[].

answered May 25, 2013 at 19:34

Comments

0

Build your items in a List<string> then use LINQ's .ToArray () to convert it into an array.

answered May 25, 2013 at 19:36

1 Comment

Note that List.ToArray() isn't a component of LINQ.
0

How about using Linq?

var dirParam = Enumerable.Range(0, 4)
 .Select(i => Request.Params["locationDirection_" + i])
 .Where(s => !String.IsNullOrEmpty(s))
 .SelectMany(s => s.Split(','))
 .ToArray();
answered May 25, 2013 at 20:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.