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.
-
2Why not use a List, then convert to array?Eugen Rieck– Eugen Rieck2013年05月25日 19:33:56 +00:00Commented May 25, 2013 at 19:33
-
Do you mean prepend? Or do you really mean append?Matthew Watson– Matthew Watson2013年05月25日 20:06:59 +00:00Commented May 25, 2013 at 20:06
4 Answers 4
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
Thomas Levesque
294k73 gold badges639 silver badges769 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use a List<string> and then the ToArray() method to convert it into a string[].
answered May 25, 2013 at 19:34
Ondrej Tucny
28.2k6 gold badges73 silver badges93 bronze badges
Comments
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
Matt Houser
36.3k6 gold badges78 silver badges93 bronze badges
1 Comment
VisualMelon
Note that
List.ToArray() isn't a component of LINQ.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
I4V
35.4k6 gold badges72 silver badges82 bronze badges
Comments
lang-cs