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

1 Answer

0 votes
import Foundation

// By using a CharacterSet that includes everything except 
// letters and numbers, we can naturally split the string while 
// correctly identifying that 世界 is a word and 、 is a separator.

/**
 * Returns the word before the last word.
 * Uses Swift's native Unicode-aware CharacterSet to handle international scripts.
 */
func getWordBeforeLast(_ text: String) -> String {
    // 1. Define a character set of everything we want to keep (Letters and Numbers).
    // 2. Invert it to get a set of "separators" (punctuation, whitespace, etc.).
    let separators = CharacterSet.letters.union(.decimalDigits).inverted
    
    // 3. Split the string by those separators.
    //    omittingEmptySubsequences handles multiple spaces/punctuation automatically.
    let words = text.components(separatedBy: separators)
                    .filter { !$0.isEmpty }

    // Check if we have at least two words
    guard words.count >= 2 else {
        return "null"
    }

    // Return the second to last word
    return words[words.count - 2]
}

func main() {
    print("=== Testing: Get Word Before Last ===\n")

    let tests = [
        "python swift",
        "  many   spaces   here   now  ",
        "OneWord",
        "",
        "   ",
        "Hello, world!",
        "Tabs\tand\nnewlines work too",
        "Unicode 世界、こんにちは",
        "Ends with punctuation.",
        "Multiple words, with punctuation, here!",
        "state-of-the-art program example"
    ]

    for t in tests {
        let result = getWordBeforeLast(t)
        
        print("Input: \"\(t)\"")
        print("Output: \(result)")
        print(String(repeating: "-", count: 40))
    }
}

main()


/*
OUTPUT:

=== Testing: Get Word Before Last ===

Input: "python swift"
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 29 by avibootz

Related questions

...