C# 使用 .ToArray() 将字符串列表转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14450213/
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
Converting a list of Strings to an array using .ToArray()
提问by user1993843
When using the .ToArray()
function is it necessary to implicitly define the size of the array in order to hold of the characters that were in the list you're converting?
使用该.ToArray()
函数时,是否有必要隐式定义数组的大小以保存要转换的列表中的字符?
String [] array = List.ToArray();
I tried doing this because I needed to use the .GetValue()
ability of the array, however, the array is maintaining a size of 1 and not holding the material from the list. Am I trying to use the .ToArray()
incorrectly?
我尝试这样做是因为我需要使用.GetValue()
数组的功能,但是,数组的大小保持为 1,并且不包含列表中的材料。我是否试图.ToArray()
错误地使用?
colAndDelimiter = new List<string>();
colAndDelimiter.Add(text);
String [] cd = colAndDelimiter.ToArray();
This is all of the code I have that effects the array. When I Console.WriteLine()
the list it gives me the entirety of the text. I may be confused about how list works. Is it storing everything as a single item, and that's why the array only shows one place?
这是影响数组的所有代码。当我Console.WriteLine()
列出清单时,它给了我全文。我可能对列表的工作方式感到困惑。是否将所有内容都存储为单个项目,这就是数组仅显示一个位置的原因?
回答by sa_ddam213
It should work fine, but try the var
operator to be sure.
它应该可以正常工作,但请尝试var
操作员以确保。
var array = List.ToArray();
Is there a reason to use Array.GetValue
instead of the built in functions of the List<T>
itself
EG:
是否有理由使用EG 本身Array.GetValue
而不是内置函数List<T>
:
string value = List.ElementAt(1);
var values = List.GetRange(0, 5);
回答by mikesjawnbit
As long as your List
object is some IEnumerable (or similar) that has items in it, this should indeed convert your List to an Array.
只要您的List
对象是一些 IEnumerable(或类似的),其中包含项目,这确实应该将您的列表转换为数组。
Note that once you have created this array, adding elements to List won't also add it to the Array
请注意,一旦创建了此数组,向 List 添加元素不会同时将其添加到 Array
回答by Jeff
You don't need to convert it to an array to get specific characters out.
您无需将其转换为数组即可获取特定字符。
Just use text[index]
to get at the needed character.
只是text[index]
用来获得所需的字符。
If you really need it as an array, use String.ToCharArray()
- you want an array of char
not an array of string
.
如果你真的需要它作为一个数组,使用String.ToCharArray()
- 你想要一个数组而char
不是string
.
Edit:
编辑:
Is it storing everything as a single item, and that's why the array only shows one place?
是否将所有内容都存储为单个项目,这就是数组仅显示一个位置的原因?
Yes, yes it is. You're making a list of string
s which contains one string
: the entire contents of text
- what it seems that you want is to split it up letter by letter, which is what the above methods will achieve.
是的,是的。您正在制作一个string
s列表,其中包含一个 sstring
的全部内容text
- 您似乎想要的是将其逐个拆分,这就是上述方法将实现的目标。
回答by MethodMan
What you are doing is fine.. but lets say that you are not sure of the size of the
string[] cd
then you can do something like the following
你在做什么很好..但是可以说你不确定的大小
string[] cd
然后你可以做如下的事情
var colAndDelimiter = new List<string>();
colAndDelimiter.Add(text);
String[] cd = { };
cd = colAndDelimiter.ToArray();
find the position of the data in cd string value = cd[0];
找到数据在cd中的位置 string value = cd[0];
Update:
If you want to do this based on values being stored in a single line then you can do this without having to declare the cd
variable as string[] cd;
更新:如果您想根据存储在一行中的值来执行此操作,那么您无需将cd
变量声明为 string[] cd;
var colAndDelimiter = new List<string>();
colAndDelimiter.Add("Hello, World, Week, Tuesday");
var cd = colAndDelimiter[0].Split(',');