How to get the word before the last word from a string (edge‑case‑safe) in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text.RegularExpressions

Module Program

    Sub Main()
        Console.WriteLine("=== Testing: Get Word Before Last ===")
        Console.WriteLine()

        ' Test cases to validate all edge conditions
        Dim testCases() As String = {
			"python vb.net",
            "  many   spaces   here   now  ",
            "OneWord",
            "",
            "   ",
            "Hello, world!",
			"Tabs" & Convert.ToChar(9) & "and" & Environment.NewLine & "newlines work too",
            "Unicode 世界、こんにちは",
            "Ends with punctuation.",
            "Multiple words, with punctuation, here!",
            "state-of-the-art program example"
        }

        For Each test In testCases
            Dim result As String = GetWordBeforeLast(test)

            Console.WriteLine($"Input: ""{test}""")
            Console.WriteLine("Output: " & If(result, "null"))
            Console.WriteLine(New String("-"c, 40))
        Next
    End Sub

    ''' <summary>
    ''' Returns the word before the last word in a string.
    ''' Handles Unicode, punctuation, multiple spaces, tabs, and edge cases.
    ''' </summary>
    Function GetWordBeforeLast(text As String) As String
        If String.IsNullOrWhiteSpace(text) Then
            Return Nothing
        End If

        ' Extract words (letters only)
        ' \p{L}+ handles Unicode letters
        Dim matches = Regex.Matches(text, "\p{L}+")

        ' Need at least two words to return the one before the last
        If matches.Count < 2 Then
            Return Nothing
        End If

        ' Return the second-to-last word
        Return matches(matches.Count - 2).Value
    End Function

End Module



' run:
'
' === Testing: Get Word Before Last ===
' 
' Input: "python vb.net"
' Output: python
' ----------------------------------------
' Input: "  many   spaces   here   now  "
' Output: here
' ----------------------------------------
' Input: "OneWord"
' Output: null
' ----------------------------------------
' Input: ""
' Output: null
' ----------------------------------------
' Input: "   "
' Output: null
' ----------------------------------------
' Input: "Hello, world!"
' Output: Hello
' ----------------------------------------
' Input: "Tabs    and
' newlines work too"
' Output: work
' ----------------------------------------
' Input: "Unicode 世界、こんにちは"
' Output: 世界
' ----------------------------------------
' Input: "Ends with punctuation."
' Output: with
' ----------------------------------------
' Input: "Multiple words, with punctuation, here!"
' Output: punctuation
' ----------------------------------------
' Input: "state-of-the-art program example"
' Output: program  
' 

 



answered Mar 28 by avibootz
edited Mar 28 by avibootz

Related questions

...