Solved

vb.net how to split a string with commas

Posted on 2013-11-07
3
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 48
FTP recursive download files 5 26
Select coumns form Data table? 3 25
Datatable / Dates ? 4 29
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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