Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,235 questions

56,138 answers

573 users

How to split a string on multiple single‑character delimiters (and keep them) in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Module SplitKeepDelimsProgram

    Function SplitKeepDelims(s As String, delimiters As String) As List(Of String)
        Dim result As New List(Of String)()

        ' Build regex: e.g. ",;|" → "([,;|])"
        Dim pattern As String = "([" & Regex.Escape(delimiters) & "])"
        Dim re As New Regex(pattern)

        Dim lastEnd As Integer = 0

        For Each m As Match In re.Matches(s)
            ' Add text before delimiter
            If m.Index > lastEnd Then
                result.Add(s.Substring(lastEnd, m.Index - lastEnd))
            End If

            ' Add the delimiter itself
            result.Add(m.Value)

            lastEnd = m.Index + m.Length
        Next

        ' Add remaining text after last delimiter
        If lastEnd < s.Length Then
            result.Add(s.Substring(lastEnd))
        End If

        Return result
    End Function

    Sub Main()
        Dim input As String = "aa,bbb;cccc|ddddd"
        Dim parts = SplitKeepDelims(input, ",;|")

        For Each p In parts
            Console.Write("[" & p & "] ")
        Next
    End Sub

End Module



' run:
'
' [aa] [,] [bbb] [;] [cccc] [|] [ddddd] 
'

 



answered Mar 9 by avibootz

Related questions

...