-2

I have a code like below which is initializing an array arrayEdgeFilterRanges through splitting an string edgeFilterRanges using:

string[] arrayEdgeFilterRanges =  edgeFilterRanges.Split(new string[] {}, StringSplitOptions.RemoveEmptyEntries);

but I need to add a general item as 0 to the first of array. I know that I can not use the arrayEdgeFilterRanges.Insert(0, "0"); since the array got fixed size on initializing statement

How can I add the item 0 at first of array?

string edgeFilterRanges = "4,2,1";
string[] arrayEdgeFilterRanges = edgeFilterRanges.Split(new string[] {}, StringSplitOptions.RemoveEmptyEntries);


foreach (string i in arrayEdgeFilterRanges)
{
    System.Console.Write(i);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

5 Answers5

2

One of many options:

var arrayWithZero = Enumerable.Repeat(“0”, 1)
                              .Concat(array)
                              .ToArray();
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • That is really terrible and won't create the requested answer - consider the type of `arrayEdgeFilterRanges` is `string[]` and the type of `Enumerable.Repeat(0, 1)` is `IEnumerable`. – NetMage Nov 29 '17 at 19:14
  • @NetMage I wouldn’t go so far as calling it terrible, that hack of prepending a string is worse from a readability point of view. This reads well and it’s clear what it does, performance isn’t an issue here and if it is then there are obviously better solutions. About the type mismatch, yeah, I got fixated on ints due to the example, stupid mistake. – InBetween Nov 29 '17 at 19:17
  • I think prepending a string is much clearer from a readability point of view. It is simple, direct, and only creates one array and requires only one extra function call. – NetMage Nov 29 '17 at 19:19
  • @NetMage I disagree. This solutions makes it obvious you want to split and existing string as then to whatever it returns, insert a given value before it. Prepending a string hides this somewhat, it’s not obvious the condition you want is all about the array, not about the input string. Subtle but there nonetheless. – InBetween Nov 29 '17 at 19:23
0

Never mind guys I just find a simple hack for this for now

edgeFilterRanges = "0,"+edgeFilterRanges;
  string[] arrayEdgeFilterRanges = edgeFilterRanges.Split(new string[] {}, StringSplitOptions.RemoveEmptyEntries);


foreach (string i in arrayEdgeFilterRanges)
{
    System.Console.Write(i);
}
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • No need to change `edgeFilterRanges` - just split on the concatenated string: `("0,"+edgeFilterRanges.Split(`... Also, your empty `string` array won't split on comma. – NetMage Nov 29 '17 at 19:12
0

Arrays themselves cannot be changed after they are created, but you could create a new array of a larger size and use Array.ConstrainedCopy() or Array.CopyTo() to copy the values over. This has really poor performance, however.

Alternatively, if you're only using the array in the foreach loop, you could use the LINQ .Concat() method to iterate two seperate arrays as if they were one.

string[] prefix = new []{"0"};
foreach (string i in prefix.Concat(arrayEdgeFilterRanges)
{ ... }
John M. Wright
  • 4,477
  • 1
  • 43
  • 61
0

Suggested simplification for answer:

string[] arrayEdgeFilterRanges = ("0,"+edgeFilterRanges).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
NetMage
  • 26,163
  • 3
  • 34
  • 55
0

You can do this using LINQ using Concat. Using your code, that would be:

string[] arrayEdgeFilterRanges = 
    new string[1] { "0" }.Concat(
        inString.Split(new string[] {}, StringSplitOptions.RemoveEmptyEntries)).ToArray();

Similar solution here.

J_Kay
  • 104
  • 5