.NET: How to trim each string with in a delimited string
Here is a function that will trim spaces in front and after of each string within a delimited string.

0 Comments   Posted by retroman80s on Feb 07, 2010   219 Views   Report Abuse
Tags: vb.net, .net, string, join, split

Bookmark and Share
Code


    ''' <summary>
    ''' This function trims the space from each string in a delimited string and returns a clean delimited string. 
    ''' You can also change the output string delimitor by passing in a different delimitor in the OutputDelimitor parameter
    ''' </summary>
    ''' <param name="DelimitedString">Delimited string to trim</param>
    ''' <param name="InputDelimitor">Input delimitor</param>
    ''' <param name="OutputDelimitor">Ouput delimitor for new delimited string</param>
    ''' <returns>New delimited string</returns>
    ''' <remarks></remarks>
    Function TrimStringsInDelimitedString(ByVal DelimitedString As String, ByVal InputDelimitor As String, ByVal OutputDelimitor As String) As String
        Dim stringArray As String()
        Dim i As Integer = 0

        stringArray = DelimitedString.Split(InputDelimitor)

        For i = 0 To UBound(stringArray)
            stringArray(i) = stringArray(i).Trim()
        Next

        Return String.Join(OutputDelimitor, stringArray)
    End Function



How to use


        Dim stringToSplit As String = "word with    spaces, vb.net, c# , Microsoft Visual Studio, Adsense , make MONEY, Sharepoint "

        'Returns a new string that is semicolon delimited
        Dim newString As String = TrimStringsInDelimitedString(stringToSplit, ",", ";")


Output


word with    spaces;vb.net;c#;Microsoft Visual Studio;Adsense;make MONEY;Sharepoint





Comments

Login or Register to add a comment.