Solved

vb.net how to split a string with commas

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

830 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