Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I concatenate the array content with its index value

I want to concatenate the index value of array with its content and then save it in a new array.For example I have the array of distances arr[]={"1.3","1.5","4.6"}

I want to concatenate these distance values with their index values and store it in new array.. I want my new Array be like: new[]= {"1.3:0","1.5:1","4.6:2"} is it possible and if yes then please tell how.. i have searched the google and find 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 don't have any comma for split function. what can be the solution for it ?

like image 351
Farwa Kazmi Avatar asked Dec 09 '25 13:12

Farwa Kazmi


1 Answers

The Split is to form the string[] from a string. If you already have the string[] then all you need is:

var result = yourArray.Select((item,index) => $"{item}:{index}").ToArray();

See that the $ is C#6.0 String Interpolation. If you want instead you can just use simple string concatenations or string.Format instead

like image 98
Gilad Green Avatar answered Dec 11 '25 03:12

Gilad Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!