Java Split() vs .NET Split()

MeMe 21 Reputation points
2021-10-03T02:48:54.593+00:00

Hello,

While using the Split() function I noticed the function behaves differently in Java and VB.NET. The returned array length is different. I tried using the regex split function, but I the bahvior is still not same as the java one. I was wondering if there is a method to force .NET to behave the same way as the Java one? or I have to write my own hack code?

Java Code :

                String[] test = "a".split("a");
                System.out.println("a - Array Length : " + test.length);

                String[] test2 = "ab".split("a");
                System.out.println("ab - Array Length : " + test2.length);

                String[] test3 = "ba".split("a");
                System.out.println("ba - Array Length : " + test3.length);

                String[] test4 = "cab".split("a");
                System.out.println("cab - Array Length : " + test4.length);

                String[] test5 = "caba".split("a");
                System.out.println("caba - Array Length : " + test5.length);


a - Array Length : 0
ab - Array Length : 2
ba - Array Length : 1
cab - Array Length : 2
caba - Array Length : 2

VB.NET

                dim test() As String = "a".Split("a")
                 Console.WriteLine("a - Array Length : " & test.length)

                dim test2() As String = "ab".split("a")
                 Console.WriteLine("ab - Array Length : " & test2.length)

                dim test3() As String = "ba".split("a")
                 Console.WriteLine("ba - Array Length : " & test3.length)

                dim test4() As String = "cab".split("a")
                 Console.WriteLine("cab - Array Length : " & test4.length)

                dim test5() As String = "caba".split("a")
                 Console.WriteLine("caba - Array Length : " & test5.length)

a - Array Length : 2
ab - Array Length : 2
ba - Array Length : 2
cab - Array Length : 2
caba - Array Length : 3

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,193 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,562 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-10-03T04:01:20.89+00:00

    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
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MeMe 21 Reputation points
    2021-10-03T16:43:54.017+00:00

    Hi Paul,

    Thanks for the quick reply. I've tested your Split() method in my code and it's working perfectly just like the Java one :) .

    Thanks

    0 comments No comments