h-rai
h-rai

Reputation: 3964

String split with two single quotes

I have a bare string where each word is separated by a comma between two single quotes.

Dim str As String = "a','b','c','d','e"

I want to split the string using ',' so that I have an array as follows:

["a", "b", "c", "d", "e"]

My code is as follows:

str.Split("','")

The array that is returned is ["a", ",", "b", ",", "c", ",", "d", ",", "e"].

I didn't expect this behaviour and am looking for an explanation of how the string is being split.

Upvotes: 3

Views: 2095

Answers (4)

Jayamurugan
Jayamurugan

Reputation: 1

Its Working Well, Try it...

   Note: Imports System.Text.RegularExpressions

  Public Shared Function SplitStr(ByVal List As String) As String

    Dim col As MatchCollection = Regex.Matches(List, "'(.*?)'")
    Dim newList As String = ""

    For Each m As Match In col
        Dim g As Group = m.Groups(1)
        If newList = "" Then
            newList = g.Value
        Else
            newList += "," & g.Value
        End If
    Next
    Return newList
End Function

Upvotes: 0

Matt Wilko
Matt Wilko

Reputation: 27322

The reason for the unexpected result is the fact that you are passing a String as the argument to Split.

There is no such overload of Split that accepts a String so because you have Option Strict off, the compiler uses the Split(Char) overload, taking only the first character in the string. So in your case

String.Split("','")

is the same as

String.Split("'")

You want to switch Option Strict On, then your code will not compile (this is a good thing because it avoids mistakes like this).

To achieve what you want, you have to pass an array of strings into the method (in this case an array containing just one string):

    Dim input As String = "a','b','c','d','e"
    Dim splitChars() As String = {"','"}
    Dim output As String() = input.Split(splitChars, StringSplitOptions.None)

Upvotes: 3

Marco
Marco

Reputation: 23937

You can do it without regex. Try this:

    Dim str As String = "a','b','c','d','e" 'note that ',c was changed to ','c

    Dim seperator() As String = New String() {"','"}
    Dim result() As String = str.Split(seperator, StringSplitOptions.RemoveEmptyEntries)

I've tried this with c# with this code:

string source = "a','b','c','d','e";
var seperator = new string[] {"','"};
var result = source.Split(seperator, StringSplitOptions.RemoveEmptyEntries);

and then converted this via http://www.developerfusion.com/tools/convert/csharp-to-vb/, so the vb.net syntax might be sketchy.

Result:

Image from LinqPad

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460138

You don't need regex, you can use Split(","c) and remove all ' with String.Trim:

Dim result = str.Split(","c).Select(Function(s) s.Trim("'"c)).ToArray()

Result: a,b,c,d,e

Side-note: with Option Strict to Off Split("','") splits only by the first character and not by the whole sub-string. If you want that you need to use the overload of String.Split.

For example(note that {"','"} is a string()):

str.Split({"','"}, StringSplitOptions.None)

With Option Strict On that would not even have compiled, which is a good thing because there is no overload. So i recommend to set Option Strict to On by default. You'll learn a lot about the framework and the .NET types by fixing the compiler errors. You'll also learn to write more robust code in that way.

Upvotes: 3

Related Questions