简体   繁体   English

使用“,”分割字符串并存储在数组列表中

[英]split the string using “,” and store in array list

  1. Here is the string in C# : string Id = "1LE, 1GE"; 这是C#中stringstring Id = "1LE, 1GE";
  2. Split the string using comma . 使用逗号 Split string
  3. Store it in ArrayList . 将其存储在ArrayList

My code is not working on online compiler. 我的代码不适用于在线编译器。

string Id = "1LE, 1GE";
ArrayList centerIds_list = new ArrayList();
centerIds_list.AddRange(centerIds);

I need the output as 2 elements with spilited string . 我需要将输出作为带有spilied string的 2元素。

Well, string.Split returns array - string[] so if you are not going to add / remove items, just put 好吧, string.Split返回数组 string.Split string[]因此,如果您不打算添加/删除项目,只需放入

  string[] centerIds_list = Id.Split(',');

If you want to add/remove items into/from the collection (and that's why array is out of question), try List<string> instead of obsolete ArrayList : 如果要向集合中添加项目或从集合中删除项目(这就是为什么数组不成问题的原因),请尝试使用List<string>而不是过时的 ArrayList

   List<string> centerIds_list = new List<String>(Id.Split(','));

Finally, if you insist on ArrayList : 最后,如果您坚持使用ArrayList

   ArrayList centerIds_list = new ArrayList(Id.Split(','));

Few more details: 更多细节:

"1LE, 1GE".Split(',') will return "1LE" , " 1GE" , please, note leading space . "1LE, 1GE".Split(',')将返回"1LE"" 1GE" ,请注意前导空格 You can get rid of it either by splitting on ", " (not ',' ): 您可以通过分割", " (不是',' )来摆脱它:

   Id.Split(new string[] { ", "}, StringSplitOptions.None);

Or with a help of Linq : .Select(item => item.Trim()) followed by materialization ( .ToArray() or .ToList() ), eg 或在Linq的帮助下: .Select(item => item.Trim())然后实现.ToArray().ToList() ),例如

   string[] centerIds_list = Id
     .Split(',')
     .Select(item => item.Trim())
     .ToArray();

Or 要么

   List<string> centerIds_list = centerIds_list = Id
     .Split(',')
     .Select(item => item.Trim())
     .ToList();

Or 要么

   ArrayList centerIds_list = new ArrayList(Id
     .Split(',')
     .Select(item => item.Trim())
     .ToArray());

You are missing line 您缺少行

var centerIds = Id.Split(',');

You need to split string by comma and then create instance of ArrayList . 您需要用comma分割字符串,然后创建ArrayList实例。

Try below code 试试下面的代码

string Id="1LE, 1GE";
var centerIds = Id.Split(','); //This was missing
ArrayList centerIds_list = new ArrayList();
centerIds_list.AddRange(centerIds);

As most of the comments raised this point, I want to include it as well. 由于大多数评论都提到了这一点,因此我也想将其包括在内。

Use List<string> instead of ArrayList . 使用List<string>而不是ArrayList

List<string> centerIds_list = "1LE, 1GE".Split(',').ToList(); //One line solution

If you want to get rid of white spaces, then use Trim() 如果要摆脱空格,请使用Trim()

List<string> centerIds_list = "1LE, 1GE".Split(',').Select(x => x.Trim()).ToList();

These days nobody uses ArrayList , instead they use a strongly typed generic. ArrayList ,没有人使用ArrayList ,而是使用强类型的泛型。 In your case that would be List<string> . 您的情况将是List<string>

To get the parts as a List<string> you can do: 要将零件作为List<string> ,可以执行以下操作:

Id.Split(',').ToList();

If you want to remove any spacing around the individual parts then you can do: 如果要删除各个零件之间的间距,则可以执行以下操作:

Id.Split(',').Select(s => s.Trim()).ToList();

string Ids="1LE, 1GE"; 字符串Ids =“ 1LE,1GE”; List centerIds_list = Ids.Split(',').Select(x => x.Trim()).ToList(); 列出centerIds_list = Ids.Split(',')。Select(x => x.Trim())。ToList(); foreach (var item in centerIds_list) { foreach(centerIds_list中的可变项){

           Console.WriteLine(item);  

        }

I know this is not answering the question per se, but if performance is an issue, and you are working on a large about of data to split (eg a large csv file), then take a look at this article as it talks about using Span as an alternative to String.split. 我知道这本身并不能解决问题,但是如果性能是一个问题,并且您正在处理大量要拆分的数据(例如,较大的csv文件),那么请看一下本文中有关使用的内容Span替代String.split。

[ https://ladeak.wordpress.com/2019/06/21/the-power-of-span/amp/?__twitter_impression=true] [ https://ladeak.wordpress.com/2019/06/21/the-power-of-span/amp/?__twitter_impression=true]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM