so I have this check combo box data source from SQL Server
Let's say I have these string value from the combo box
ABC, DEF, GHI
What I wanted next is get those string
and make them become like this
'ABC', 'DEF', 'GHI'
I've tried combining them with "'" & comboBox.Text & "'"
but it looks like this
'ABC, DEF, GHI'
CodePudding user response:
You're starting with a delimited string and you want to process each substring separately, so that means splitting, processing and joining the string. There are a number of variations on a theme that will do that. Here are a couple of examples:
Dim substrings = comboBox.Text.Split(","c)
For i = 0 To substrings.getUpperBound(0)
substrings(i) = "'" & substrings(i).Trim() & "'"
Next
Dim result = String.Join(", ", substrings)
Dim result = String.Join(", ",
comboBox.Text.
Split(","c).
Select(Function(s) $"'{s.Trim()}'"))
Note that, if you're confident that there will always be a comma and one space between substrings then you can incorporate the space into the Split
and do away with the Trim
:
Dim substrings = comboBox.Text.Split({", "}, StringSplitOptions.None)
For i = 0 To substrings.getUpperBound(0)
substrings(i) = "'" & substrings(i) & "'"
Next
CodePudding user response:
Well, you can do it LINQ style like:
Dim res as string = String.Join(“, “, String.Split(“,”c, original).Select(Function(f) $”’{f.Trim}’”))
The process is to split the original into its component parts, remove any white space, add surrounding quotes and combine back into a single string with “, “ as a separator.