如何删除字符串数组的第一个元素 [英] How can I remove first element of string array
本文介绍了如何删除字符串数组的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组,其中最多有4个字符串
Hi,
I have a string array in which there are max of 4 strings
string[] userInput = new string[4];
我需要消除数组的第一个元素,并将剩余的元素分配给另一个字符串数组,如下所示
I need to eliminate first element of the array and assign the remaining elements to another string array like below
GWmsgDetails.GatewayBuses = userInput;
br mode =hold/>任何人都可以告诉我,我怎么能为此编写代码
谢谢
John
br mode="hold" />Can anyone please tell me, how can I wrote code for this
Thanks
John
推荐答案
尝试:
Try:
string[] userInput = new string[] { "hello", "there", "world"};
string[] withFirstBitMissing = new string[userInput.Length - 1];
Array.Copy(userInput, 1, withFirstBitMissing, 0, withFirstBitMissing.Length);
使用linq的另一个解决方案:
another solution by using linq:
string[] arr1 = new string[4] { "1", "2", "3", "4" };
string[] arr2 = arr1.Skip(1).ToArray();
for循环解决方案
string [] userInput = new string [4] {1 ,2,3,4};
string [] userOutput = new string [userInput.Length -1];
for(int i = 1,j = 0; i< userInput.Length; i ++,j ++)
userOutput [j] = userInput [i];
Solution by for loop
string[] userInput = new string[4] {"1","2","3","4"};
string[] userOutput = new string[userInput.Length -1 ];
for (int i = 1, j = 0 ; i < userInput.Length; i++ , j++)
userOutput[j] = userInput[i];
这篇关于如何删除字符串数组的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文