Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27360

[RESOLVED] Help Split String

$
0
0
Hi, I am making a CSV class for reading and updating CSV file. Anyway I use a little split function I found ages ago, It works well splits string with comma's , and can also split strings with quotes. The problem it uses RegularExpressions to be honest I not that good with RegularExpressions, Anyway what I want is to have my string function split on anything say split a string like.

Ben;Jones
or
Ben|Jones

I left my function below so you guys can have a look and maybe help me fix to what I want to do Thanks.

Code:

    Private Function SplitString(ByVal Source As String) As String()
        Dim Tmp(0) As String
        Const VBQuote As String = """"

        Dim Patten As String = ",(?=(?:[^\" + VBQuote + "]*\" + VBQuote +
            "[^\" + VBQuote + "]*\" + VBQuote + ")*(?![^\" + VBQuote + "]*\" + VBQuote + "))"

        If String.IsNullOrEmpty(Source) Then
            SplitString = Tmp
        Else
            'Split the string and return as array.
            SplitString = System.Text.RegularExpressions.Regex.Split(Source, Patten)
        End If

    End Function

How to use function.
Code:

        Dim sText() As String = SplitString("Ben,Jones")
        'Example
        MsgBox(sText(0)) 'Ret Ben
        MsgBox(sText(1)) 'Ret Jones


Viewing all articles
Browse latest Browse all 27360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>