Transform a List(of String) into a single string, with custom formatting

Published


Sometimes when working with a List(Of String), or IEnumerable(Of String), we need to combine the String values into one for display on the page.  This is usually achieved very easily using String.Join.

Taking this simple list, we will try and join it back together to achieve different results:

Dim l As New List(Of String) From {"Arnold", "Sylvester", "Bruce", "Dolph", "Mr T"}

String.Join is just too simplistic...!

To join the list items back together using a separator character, we can do this:

MyLiteral.Text = String.Join(", ", l)

The result is this:

Arnold, Sylvester, Bruce, Dolph, Mr T

However, this week I needed to join a list in this manner, but apply HTML code to the beginning and the end of each item, rather than simply just combining them using a separator.  After a lot of searching, it appears there is no simple way to do this out of the box.

In my case, I need to create bullet list items from a List(Of String).  Notice that we are adding HTML to both sides of each list item, plus the outer <ul> tag around the joined string, e.g:

<ul>
    <li>Arnold</li>  <!-- each item is surrounded but start and end <li> tags -->
    <li>Sylvester</li>
    <li>Bruce</li>
    <li>Dolph</li>
    <li>Mr T</li>
</ul>  <!-- the whole joined string is also wrapped in a parent <ul> tag -->

However, thanks to Stack Overflow, the answer came quickly.  We can write a very simple extension method to achieve this.  The ASP.Net framework is specifically designed to make this type of thing simple.

Joining Strings from a List into Formatted Strings

In the App_Code folder in your application, create a Class file with this content:

Imports System.Runtime.CompilerServices

Public Module MyModules

    <Extension()> _
    Public Function JoinFormat(ie As IEnumerable(Of String), formatString As String) As String
        Return String.Join(String.Empty, ie.Select(Function(s As String) String.Format(formatString, s)))
    End Function

End Module

If you're not a fan of Linq, you can use this equivalent method in place of the above:

Imports System.Runtime.CompilerServices

Public Module MyModules

    <Extension()> _
    Public Function JoinFormat(ie As IEnumerable(Of String), formatString As String) As String
        Dim sb As New StringBuilder
        For Each item As String In ie.ToList()
            sb.Append(String.Format(formatString, item))
        Next
        Return sb.ToString()
    End Function

End Module

Now within Visual Studio, the extension becomes available to use on the List (note that the method description won't be there; I removed it from my code example above  to make it more user-friendly):

Join lists (of string) with formatting 

So we can create a list extremely simply now, with just this:

MyLiteral.Text = String.Format("<ul>{0}</ul>", l.JoinFormat("<li>{0}</li>"))

Which returns this (line breaks added for clarity):

<ul>
<li>Arnold</li>
<li>Sylvester</li>
<li>Bruce</li>
<li>Dolph</li>
<li>Mr T</li>
</ul>

You could create paragraphs from string lists instead by altering the code to this:

MyLiteral.Text = l.JoinFormat("<p>{0}</p>")

The result is now:

<p>Arnold</p><p>Sylvester</p><p>Bruce</p><p>Dolph</p><p>Mr T</p>

Simple and straightforward!

Comments

No comments have been posted. Be the first to write a comment...

New Comment





This is to prove that you are not a spam robot posting links to dodgy sites!