?
Solved

vb.net how to split a string with commas

Posted on 2013-11-07
3
Medium Priority
?
970 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 2000 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 86

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

864 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