Solved

vb.net how to split a string with commas

Posted on 2013-11-07
3
679 Views
Last Modified: 2013-11-07
I have a string and I need to add commas in between each character. For example
String s = ABCD
new string = A,B,C,D
0
Comment
Question by:Meinhoonaa
3 Comments
 
LVL 15

Accepted Solution

by:
unknown_routine earned 500 total points
ID: 39631454
Dim s As String = "ABCD"
        Dim m As String = ""

        For i As Integer = 0 To s.Length - 1
            If i < s.Length - 1 Then
                m = m + s(i) & ","
            Else
                m = m + s(i)
            End If
        Next

        s = m
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39631642
Here's a quick one-liner:
        Dim s As String = "ABCD"
        Dim newS As String = String.Join(",", Array.ConvertAll(s.ToCharArray, Function(x) x.ToString))
        Debug.Print(s)
        Debug.Print(newS)

Open in new window


Here it is as an extension method:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim s As String = "ABCD"
        Dim newS As String = s.SeparateWith(",")
        Debug.Print(s)
        Debug.Print(newS)
    End Sub

End Class

Public Module Module1

    <Runtime.CompilerServices.Extension()>
    Public Function SeparateWith(ByVal s As String, ByVal delimiter As String) As String
        Return String.Join(delimiter, Array.ConvertAll(s.ToCharArray, Function(x) x.ToString))
    End Function

End Module

Open in new window

0
 

Author Closing Comment

by:Meinhoonaa
ID: 39631724
thank u
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question