1

I'm pretty new to vb.net and I don't understand why split works differently if separator is more then one character.

I've tryed this on .net fiddle and I was surprised from result:

Dim Txt as string = "123_|_ABC_|_sd"
Dim c() as string

c= Txt.Split("_|_")

console.WriteLine(c.length)
For i = 0 to c.length -1
    console.WriteLine(c(i))
Next
Txt = "123|ABC|sd"  
c= Txt.Split("|")

console.WriteLine(c.length)
For i = 0 to c.length -1
    console.WriteLine(c(i))
Next

Result was, for first part of code:

5
123
|
ABC
|
sd

For the second part of code:

3
123
ABC
sd

My questions are: Why this happens? Is there a way to get result of 2nd part of code with a separator of multiple chars?

genespos
  • 3,211
  • 6
  • 38
  • 70
  • 1
    Option Strict is not on – Ňɏssa Pøngjǣrdenlarp Jun 26 '15 at 16:02
  • @Plutonix I have added "Option Strict On" on .net fiddle but nothing changed – genespos Jun 26 '15 at 16:06
  • 1
    You ought to be getting a compiler error now, as there are no overloads of `String.Split` that take a single string as a parameter. The issue is it's being cast to a character array, badly – James Thorpe Jun 26 '15 at 16:07
  • The argument to `String.Split` is Char or Char array (that is, one single character). To chop the first one, `String.Replace` `_` with `""` then split – Ňɏssa Pøngjǣrdenlarp Jun 26 '15 at 16:08
  • [This is a C# example](http://stackoverflow.com/questions/2245442/c-sharp-split-a-string-by-another-string) (so not sure we can class it as a duplicate?), but the methods involved are identical – James Thorpe Jun 26 '15 at 16:10
  • @JamesThorpe Thanks for the link (I didn't find it while searching) but I'm not able to translate in vb.net the accepted answer – genespos Jun 26 '15 at 16:27
  • @genespos Yeah the syntax for declaring an array inline is a bit different... two secs, I'll post an answer using it (though the one using regular expressions below should work) – James Thorpe Jun 26 '15 at 16:28
  • 2
    Actually... sorry - I need to dive out now so can't do a full answer, the syntax you need is: `Txt.Split(New String() {"_|_"}, StringSplitOptions.None)` – James Thorpe Jun 26 '15 at 16:30
  • @JamesThorpe Thanks, It's all I need. :) – genespos Jun 26 '15 at 16:36
  • @JamesThorpe If you'll give the code as an answer then I'll be able to accept it – genespos Jun 26 '15 at 19:02

2 Answers2

2

You can try a Regex.Split() with the pattern

"_\|_"

Example:

Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        Dim Txt As String = "123_|_ABC_|_sd"
        Dim c() As String

        c = Regex.Split(Txt, "_\|_")

        For i = 0 To c.Length - 1
            Console.WriteLine(c(i))
        Next

        Console.ReadLine()
    End Sub
End Module

Results:

123
ABC
sd
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
2

The .NET String.Split() method accepts two arguments:

  1. The string you want to split
  2. A list of characters, any of which will be recognized as a delimiter

So, this line of code:

c= Txt.Split("_|_")

Doesn't say "use | as a delimter". Rather, it says "use _ or | or _ as a delimiter".

The most straightforward way to get the behavior that you are looking for is to use the String class from the Microsoft.VisualBasic namespace. This is often sneered at, but there are cases where it simply does things better than the corresponding .NET classes.

So, import the Microsoft.VisualBaisc namespace, then...

c = Split(Txt,"_|_")
DWRoelands
  • 4,878
  • 4
  • 29
  • 42