I believe you'll need something custom to make these consistent. Java removes trailing empty strings (and also leading empty strings if the string is equal to the sub-string), whereas VB/C# always add empty strings when the sub-string is at the beginning or end of the string (unless StringSplitOptions.RemoveEmptyEntries
is passed to the Split
method.)
Here's an extension method that should help you get what you're after:
Module StringExtensions
<Extension()>
Public Function SplitJava(ByVal value As String, ByVal separator As String) As String()
Dim segments = value.Split(separator)
If (segments.LastOrDefault() = "") Then
If (segments.Length = 2 And segments.First() = "") Then
Return Array.Empty(Of String)()
End If
segments = segments.Take(segments.Length - 1).ToArray()
End If
Return segments
End Function
End Module
Usage:
Module Program
Sub Main(args As String())
Dim test = "a".SplitJava("a")
Console.WriteLine("a - Array Length : " & test.Length)
Dim test2 = "ab".SplitJava("a")
Console.WriteLine("ab - Array Length : " & test2.Length)
Dim test3 = "ba".SplitJava("a")
Console.WriteLine("ba - Array Length : " & test3.Length)
Dim test4 = "cab".SplitJava("a")
Console.WriteLine("cab - Array Length : " & test4.Length)
Dim test5 = "caba".SplitJava("a")
Console.WriteLine("caba - Array Length : " & test5.Length)
End Sub
End Module