locked
Removing first element of Array RRS feed

  • Question

  • Hi everybody,

    I found the similar old thread, but decided to start a new one. How can I concatenate elements of the array excluding the first one when the length of the array is greater than 1? Here is the original code that is not working on my test data:

    var descripArray = ticketTemplates[t].Descrip.Split('-');
    
                    ticketTemplates[t].Descrip = "[" + descripArray[1] + ", " + descripArray[2] + ", " + descripArray[3] + "]";

    This is how I re-wrote it, but I noticed that it's not the same, as the first element needs to be not concatenated:

     var descripArray = ticketTemplates[t].Descrip.Split('-');
    
                    ticketTemplates[t].Descrip = "[" + String.Join(", ", descripArray) + "]";

    So, looking for a simple way to correct my correction to remove the first element in case we have more than 1 element in that array.

    Thanks in advance.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Tuesday, August 25, 2015 10:41 PM

Answers

  • Hi. There is an overload of String.Join where you can set the start index which will let you skip the first element in the array.

    ticketTemplates[t].Descrip = "[" + String.Join(", ", descripArray, 1, 3) + "]";

    • Marked as answer by Naomi N Wednesday, August 26, 2015 12:44 AM
    Wednesday, August 26, 2015 12:12 AM

All replies

  • Naomi : Does this help ?

     var result = new StringBuilder("[");
                    result.Append(descripArray.Skip(1).Aggregate((x, y) => x + ", " + y));
                    result.Append("]");

    Tuesday, August 25, 2015 11:17 PM
  • Hi. There is an overload of String.Join where you can set the start index which will let you skip the first element in the array.

    ticketTemplates[t].Descrip = "[" + String.Join(", ", descripArray, 1, 3) + "]";

    • Marked as answer by Naomi N Wednesday, August 26, 2015 12:44 AM
    Wednesday, August 26, 2015 12:12 AM