how to take all array elements except last element in C#

后端 未结 6 820
滥情空心
滥情空心 2020-12-14 14:50

I have a string array like this.

string[] queries with data more than one string.

I want to skip the last string from the element and take the r

6条回答
  •  攒了一身酷
    2020-12-14 15:04

    Edited: You could effectively replace your array with the same array minus the last element with the following line or code:

    queries = queries.Take(queries.Length - 1).ToArray();
    

    If you would like to create a method that does this for you, then you could use the following:

    public static string[] TrimLastElement(string[] arr) {
        return arr.Take(arr.Length - 1).ToArray();
    }
    

    And implement it in your code like so:

    queries = TrimLastElement(queries);
    

提交回复
热议问题