[英]Concatenate string from array and include it's index C#
Consider the following csv 考虑以下csv
string data = "Hey, Bob, How are you";
I can flatten it to: 我可以将其展平为:
"Hey; Bob; How are you"
Using the following: 使用以下内容:
var s = String.Join("; ",data.Split(',').Select(d => d.Trim()).ToArray());
Is there any way I can get the index of the current item in the join and append it to the resulting string? 有什么方法可以获取联接中当前项目的索引并将其附加到结果字符串中? To produce somthing along the lines of:
产生东西:
"Hey=0; Bob=1; How are you=2"
Does linq facilitate anything like this? linq是否会促进此类活动? Perhaps combined with a String.Format() type method?
也许结合使用String.Format()类型的方法?
Here try this there is an index selector in the select you can use it to concatonate with each of your data pieces 在这里尝试一下,选择中有一个索引选择器,您可以使用它来合并每个数据片段
var s = String.Join("; ",data.Split(',')
.Select((d, i) => d.Trim() + "= " + i.ToString()).ToArray());
Sure - just change your Select
slightly: 当然-只需稍微更改一下“
Select
:
var s = String.Join("; ",data.Split(',')
.Select((d, i) => String.Format("{0}={1}",d.Trim(),i)));
note that string.Join
can take an IEnumerable<T>
so there's no need to call ToArray
. 请注意,
string.Join
可以采用IEnumerable<T>
因此无需调用ToArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.