Help with reformating string using VB.NET

Hi,

How do you reformat a string variable using VB.NET?  For example if S = xxx,yyy,zzz  How do I convert it to S = 'xxx','yyy','zzz' ?

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
Something like:
Module Module1
	Private Const data As String = "xxx,yyy,zzz"
	Sub Main()
		Dim temp = data.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
		Dim output = String.Empty
		For i As Integer = 0 To temp.Length - 1 Step 1
			output = If(i = 0, String.Format("'{0}'", temp(i)), String.Format("{0},'{1}'", output, temp(i)))
		Next

		Console.WriteLine(output)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0
 
Rgonzo1971Commented:
Hi,

Why not simply use join

Dim  sep As String  = "','"
Dim temp = data.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
Dim output = String.Empty
output =  "'" + String.Join(sep, temp) +"'"

Open in new window


Regards
0
 
ElrondCTCommented:
Rgonzo, String.Join won't put the quotes in between the items.

I think it's less confusing to write Saige's loop as
		For i As Integer = 0 To temp.Length - 1 Step 1
			output &= "'" & temp(i) & "'"
			If i < temp.Length - 1 Then
				output &= ","
			End If
		Next

Open in new window

but that's a matter of personal preference. Some people really like being able to put everything on a single line.
0
 
vcharlesAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.