Jon
Jon

Reputation: 425

What's the best way to remove white space after a certain character in a string?

I'm trying to build a list that will be used as the in clause of a select statement. The requirement is to have the user enter a comma separated list of descriptions. Each description can contain spaces so I can't remove the spaces before splitting by comma to add the single quotes around each description. I want to remove all white space after a single quote since no description will start with a space. What's the best way to do this in VB.NET? Regular expression or a string function? Here's what I have so far.:

Partial Class Test
    Inherits System.Web.UI.Page

    Protected Sub cmdGetParts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetParts.Click
        Dim sDescriptionList As String = ""
        BuildList(sDescriptionList)
        RemoveSpacesFromList(sDescriptionList)
        FillGrid(sDescriptionList)
    End Sub

    'Build descriptions List based on txtDescriptionList.Text
    Private Sub BuildList(ByRef sDescriptionList As String)
        Dim sDescriptionArray As String()
        sDescriptionArray = txtDescriptionList.Text.Trim.Split(","c)
        Dim iStringCount As Integer = 0
        For Each description In sDescriptionArray
            If iStringCount > 0 Then
                sDescriptionList = sDescriptionList & ","
            End If
            sDescriptionList = sDescriptionList & "'" & description & "'"
            iStringCount = iStringCount + 1
        Next
    End Sub

    **'This procedure removes unwanted spaces from  description list
    Private Sub RemoveSpacesFromList(ByRef sList As String)
        sList = sList.Replace("' ", "'")
    End Sub**

    'This procedure fills the grid with data for descriptions passed in
    Private Sub FillGrid(ByVal sDescriptionList As String)
        Dim bo As New boPart
        Dim dtParts As Data.DataTable
        dtParts = bo.GetPartByDescriptionList(sDescriptionList)
        GridView1.DataSource = dtParts
        GridView1.DataBind()
    End Sub
End Class 

Edited: After reviewing this code I think I may be able to just place description.Trim inside the For Each loop of the BuildList procedure.

Upvotes: 1

Views: 3871

Answers (2)

Guffa
Guffa

Reputation: 700212

Use a regular expression to match a comma with any surrounding white space, and replace with apostropes and a comma. The starting apostrope for the first item and the ending apostrophe for the last item you simply add afterwards.

The RemoveSpacesFromList method is no longer needed, as the BuildList method does it all.

Protected Sub cmdGetParts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetParts.Click
    Dim descriptions As String = txtDescriptionList.Text
    descriptions = BuildList(descriptions)
    FillGrid(descriptions)
End Sub

''//Build descriptions List based on a comma separated string
Private Function BuildList(ByVal descriptions As String) As String
   Return "'" + Regex.Replace(descriptions, "\s*,\s*", "','", RegexOptions.Compiled) + "'"
End Function

Note:
If you are using this string to build an SQL query, your application is wide open for SQL injection attacks. Using parameterised queries is the preferred method, but that might not be convenient in your case. User input has to at least be sanitised before being used in a query.

Edit:
If the adapter uses apostrophes as escape character in a string literal, you can escape the string properly like this:

Private Function BuildList(ByVal descriptions As String) As String
   Return "'" + Regex.Replace(descriptions.Replace("'","''"), "\s*,\s*", "','", RegexOptions.Compiled) + "'"
End Function

Upvotes: 1

JaredPar
JaredPar

Reputation: 754575

As long as you cannot have embedded single quotes, the following should do the trick

Dim replaced = Regex.Replace(input, "'\s+", "'")

The regex string '\s+ will match any single quote followed by one or more white space characters. All instances of this match will be replaced by a single quote.

Upvotes: 2

Related Questions