将数据加载到列表中抛出"foreach"语句 [英] Loading Data Into List Throw 'foreach' Statement

查看:97
本文介绍了将数据加载到列表中抛出"foreach"语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个"foreach"语句,有点像这样:

hey guys,

i have a ''foreach'' statement that is a little like this:

foreach (string a in text.Split('\n'))
{
}



我需要加载已被拆分为名为令牌的列表的所有令牌.没有太多代码,有什么办法可以做到这一点吗?请帮忙!



i need to load all the tokens that has been split up into a list called tokens. is there any way i can do this without too much code? please help!

推荐答案

string.SPlit返回字符串数组.

所以你可以有类似的东西
The string.SPlit returns an array of strings.

so you can have something like
string[] items = text.Split('\n');


并进一步使其安全(如果您知道将要发送的物品数量)


and further to make it type safe(if you know the number of items that will be coming)

enum ITEMS
{
 item0 = 0,
 tem1 = 1
}


并作为
访问


and access it as

var variable = items[ITEMS.item0];


注意:我不确定这是否会减少代码量,但这是另一种方法.


Note: I am not sure whether this will give you less code but this is an alternate way of doing it.


:doh:

试试:
:doh:

Try:
string someText = "My name is \n Sandeep Mewara. \n Thanks for asking.";
string[] textValues = someText.Split('\n');
List<string> tokens = new List<string>();
tokens = textValues.ToList();


这篇关于将数据加载到列表中抛出"foreach"语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆