Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.NET Core : how can I replace a character at int values in foreach or for loop?

I have a loop whose values ​​are int. The values ​​are displayed in this way: 14 16 18.

I want to place a dash - between the numbers of the characters: 14-16-18

I searched a lot and did not find a suitable solution 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Loop:

foreach (int size in SizeList)
{
    <span class="size">@size </span>
}

>Solution :

You may also be able to handle the task with the Join() method of the string class

<span>class="size">@(string.Join("-", sizeList.Select(i => i.ToString())))</span>

or, possibly using a more defensive style of coding if you are in danger of null reference exceptions here:

@if(sizeList is not null)
{
    <span>class="size">@(string.Join("-", sizeList.Select(i => i.ToString())))</span>
}

Actually, the explicit conversion to string is not necessary as in the answer given by @Dave Bry so you can get by with less code too:

<span>class="size">@string.Join("-", sizeList)</span>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading