Solved

vb.net how to split a string with commas

Posted on 2013-11-07
3
650 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now