Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于大字符串数组添加ListView项_C#_.net_Arrays - Fatal编程技术网

C# 基于大字符串数组添加ListView项

C# 基于大字符串数组添加ListView项,c#,.net,arrays,C#,.net,Arrays,例如,我有一个字符串[],格式如下: 1000internal_ref 838Bdoesnt_have_to_start_with_internal_ref_and_this_can_be_as_long_as_it_needs_to_be 我需要获取前四个字符(本例中为1000和838B)并将它们放在ListView中设置为“details”模式的一列(id)下,然后获取其余字符并将它们放在第二列(内部)。它会这样显示,但不是以文本形式,当然: IDs | Internals ---- |

例如,我有一个字符串[],格式如下:

1000internal_ref
838Bdoesnt_have_to_start_with_internal_ref_and_this_can_be_as_long_as_it_needs_to_be
我需要获取前四个字符(本例中为1000和838B)并将它们放在ListView中设置为“details”模式的一列(id)下,然后获取其余字符并将它们放在第二列(内部)。它会这样显示,但不是以文本形式,当然:

IDs  | Internals
---- | ---------------------------------------
1000 | internal_ref
838B | doesnt_have_to_start_with_internal_ref_and_this_can_be_as_long_as_it_needs_to_be
我的资源中有一个文本文件,其中包含应该放在ListView中的字符串。它总是10348行长,这就是为什么我专门为ListView设置的两个字符串数组那么长的原因。将使用
'\r'
拆分字符串,以便将其放入数组中。我已经看到了关于如何基于字符串数组向ListView添加项的答案,但如果它有多个列,则没有。我不太确定该怎么做,这就是我目前所拥有的

string[] foo = Properties.Resources.sfxlist.Split('\r');
string[] ids = new string[10348];
string[] inte = new string[10348];
int i = 0;
foreach (string s in foo)
{
    ids[i] = s.Take(4).ToArray().ToString(); // I've also tried doing it without ToArray(), no dice
    inte[i] = s.Skip(4).ToArray().ToString(); // see above
    i += 1;
}
1ListView.Items.Add("IDs").SubItems.AddRange(inte);
1ListView.Items.Add("Internals").SubItems.AddRange(ids);
这很有效

var myArray = new string[]{"1000internal_ref", "838Binternal_ref_this_can_be_as_long_as_it_needs_to_be"};

var list = myArray.Select(i => 
   {
       var left = i.Substring(0, i.IndexOf("internal_ref"));
       var right = i.Substring(i.IndexOf("internal_ref"));
       return new { Id = left, Internals = right };
   }).ToList();

list.ForEach(x => Console.WriteLine(x.Id + "---" + x.Internals));
1000---内部参考
838B---内部参照此项可以根据需要而定

现在,将列表绑定到列表视图

如果您的ID始终为4个字符长,请将linq更改为

var list = myArray.Select(i => 
   {
       var left = i.Substring(0, 4);
       var right = i.Substring(4);
       return new { Id = left, Internals = right };
   }).ToList();

在你的情况下,我会做以下工作:

1ListView.Columns.Add("IDs");
1ListView.Columns.Add("Internals");

1ListView.Items.AddRange(Properties.Resources.sfxlist.Split('\r')//split the value
    .Select(val => new ListViewItem(new string[]//assign a listviewitem to each value
    { val.Substring(0, IndexOf("internal_ref")),//the subitems consist of the two values
    val.Substring(IndexOf("internal_ref")) })).ToArray());
Tadaa,所有这些都在一个命令中:-)

此外,如果使用大量值,使用linq也会使其速度更快,这不是最后一个方面


请让我知道这个答案是否有用

OP是说他的所有字符串在4个整数后都以“internal_ref”开头,还是只是一个例子。也许改进答案的更好方法是使用
i.Substring(0,4)
i.Substring(4)
?@Sach有没有说过,所有ID都是4个字符长的?事实上,OP说我需要获得前四个字符(本例中为1000和838B)“在这种情况下!”让我们看看反应会是什么……是的,我相信他做到了:我需要得到前四个character@Sach“在这种情况下”!!不,“在这个例子中”是为了说明这个例子,但是OP在imo中说的很清楚。他希望前四个字符作为ID,其余的作为内部字符。所有ID都是4个字符长吗?在id之后是否总是有
internal\u ref
?@Sach
Take
不返回字符数组它返回
IEnumerable
然后
ToArray
将其转换为字符数组。如果在字符数组上调用
ToArray
,则会得到一个字符数组,而不是字符串数组。你说得对,对不起。@T.S.所有ID的长度都是4个字符
internal\u ref
只是一个例子,它并不总是在ID之后。@snorepion我已经发布了我的解决方案作为答案,但这里我想指出为什么你的
.ToArray().ToString()
不起作用;这只是
Array
类型的常规
ToString()
方法,它对字符串值数组没有任何特殊作用。您要寻找的是
.Substring(int,int)
方法,或者如果您对字符集合执行更多特殊操作,然后希望将其作为字符串返回,则可以使用字符串构造函数:
新字符串(char[])
哇,您必须是一名代码高尔夫球手。它工作得几乎完美,但出于某种原因,在第一个索引之后,它将第四个字符分隔为内部字符串。看见我确实修改了它,将“internal_ref”切换为4,因为这只是一个例子。不过,我通过将4s改为5s来修复它。我不知道为什么需要这样做,但它起了作用。嗯,这很有趣。作为输入的文本值是什么?我不打算列出所有这些值,因为有10000多个,但有一个例子是:
0013 SND\u SE\u SYSTEM\u FIXED\u L
我想它是将选项卡作为一个字符来计算的。它们的格式都是这样的。你在IndexOf方法中加入了什么?