C# 从数组中删除第一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9219958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove first element from array
提问by atwellpub
PHP developer here working with c#. I'm using a technique to remove a block of text from a large string by exploding the string into an array and then shifting the first element out of the array and turning what remains back into a string.
PHP 开发人员在这里使用 c#。我正在使用一种技术,通过将字符串分解为数组,然后将第一个元素移出数组并将剩余的元素转回字符串,从而从大字符串中删除文本块。
With PHP (an awesome & easy language) it was just
使用 PHP(一种很棒且简单的语言),它只是
$array = explode('somestring',$string);
array_shift($array);
$newstring = implode(' ', $array);
and I'm done.
我已经完成了。
I get so mad at c# for not allowing me to create dynamic arrays and for not offering me default functions that can do the same thing as PHP regarding arrays. Instead of dynamic arrays I have to create lists and predefine key structures etc. But I'm new and I'm sure there are still equally graceful ways to do the same with c#.
我对 c# 非常生气,因为它不允许我创建动态数组,并且没有为我提供默认函数,这些函数可以在数组方面做与 PHP 相同的事情。我必须创建列表和预定义键结构等,而不是动态数组。但我是新手,我确信仍然有同样优雅的方法可以用 c# 做同样的事情。
Will someone show me a clean way to accomplish this goal with c#?
有人会告诉我用 C# 实现这个目标的干净方法吗?
Rephrase of question: How can I remove the first element from an array using c# code.
问题改述:如何使用 c# 代码从数组中删除第一个元素。
Here is how far I've gotten, but RemoveAt throws a error while debugging so I don't believe it works:
这是我已经走了多远,但是 RemoveAt 在调试时抛出了一个错误,所以我不相信它有效:
//scoop-out feed header information
if (entry_start != "")
{
string[] parts = Regex.Split(this_string, @entry_start);
parts.RemoveAt(0);
this_string = String.Join(" ", parts);
}
采纳答案by Darin Dimitrov
I get so mad at c# for not allowing me to create dynamic arrays
我对 c# 非常生气,因为它不允许我创建动态数组
You may take a look at the List<T>class. Its RemoveAtmight be worth checking.
你可以看看List<T>类。它的RemoveAt可能值得检查。
But for your particular scenario you could simply use LINQ and the Skipextension method (don't forget to add using System.Linq;to your file in order to bring it into scope):
但是对于您的特定场景,您可以简单地使用 LINQ 和Skip扩展方法(不要忘记添加using System.Linq;到您的文件中以将其纳入范围):
if (entry_start != "")
{
string[] parts = Regex.Split(this_string, @entry_start).Skip(1).ToArray();
this_string = String.Join(" ", parts);
}
回答by Chris Shain
You can use LINQ for this:
您可以为此使用 LINQ:
if (entry_start != "")
this_string = String.Join(" ", Regex.Split(this_string, @entry_start).Skip(1).ToArray());
回答by Roman Royter
C# is not designed to be quick and dirty, nor it particularly specializes in text manipulation. Furthermore, the technique you use for removing some portion of a string from a beginning is crazy imho.
C# 不是为了快速和肮脏而设计的,也不是特别擅长文本操作。此外,您用于从开头删除字符串的某些部分的技术是疯狂的恕我直言。
Why don't you just use String.Substring(int start, int length)coupled with String.IndexOf("your delimiter")?
你为什么不直接使用String.Substring(int start, int length)与String.IndexOf("your delimiter")?
回答by JYelton
You might be more comfortable with generic lists than arrays, which work more like PHP arrays.
您可能更喜欢通用列表而不是数组,后者更像 PHP 数组。
But if your goal is "to remove a block of text from a large string" then the easier way would be:
但是,如果您的目标是“从大字符串中删除一段文本”,那么更简单的方法是:
string Example = "somestring";
string BlockRemoved = Example.Substring(1);
// BlockRemoved = "omestring"
Edit
编辑
I misunderstood the question, thinking you were just removing the first element from the array where the array consisted of the characters that make up the string.
我误解了这个问题,认为您只是从数组中删除第一个元素,其中数组由构成字符串的字符组成。
To split a string by a delimiter, look at the String.Splitmethod instead. Some good examples are given here.
要通过分隔符拆分字符串,请查看String.Split方法。这里给出了一些很好的例子。
回答by zeal
string split = ",";
string str = "asd1,asd2,asd3,asd4,asd5";
string[] ary = str.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
string newstr = string.Join(split, ary, 1, ary.Count() - 1);
splits at ",". removes the first record. then combines back with ","
在“,”处分开。删除第一条记录。然后与“,”组合回来
回答by John Koerner
Here is the corresponding C# code:
下面是对应的C#代码:
string input = "a,b,c,d,e";
string[] splitvals = input.Split(',');
string output = String.Join(",", splitvals, 1, splitvals.Length-1);
MessageBox.Show(output);
回答by vi3x
As stated above, you can use LINQ. Skip(int)will return an IEnumerable<string>that you can then cast back as array.
如上所述,您可以使用 LINQ。Skip(int)将返回一个IEnumerable<string>,然后您可以将其转换为数组。
string[] myArray = new string[]{"this", "is", "an", "array"};
myArray = myArray.Skip(1).toArray();

