How can I concatenate the contents of an array with its index value
I want to concatenate the index value of an array with its contents and then store it in a new array. For example, I have an array of distancesarr[]={"1.3","1.5","4.6"}
I want to combine these distance values โโwith their index values โโand store them in a new array. I want my new array to look like this: new[]= {"1.3:0","1.5:1","4.6:2"}
is it possible and if yes then tell me how .. I searched google and found this function
var s = String.Join("; ",data.Split(',')
.Select((d, i) => d.Trim() + "= " + i.ToString())
.ToArray());
but this function is for string and my array also has no comma for split function. what can be the solution for this?
source to share
Split
should form string[]
from a string
. If you already have string[]
, then all you need is:
var result = yourArray.Select((item,index) => $"{item}:{index}").ToArray();
See what $
is C # 6.0 string interpolation. If you want, you can just use simple string concatenations string.Format
instead, or instead
source to share