String[] words = s.trim().split("\\s+"); // Input -> " Hi i am om " // Output -> ["Hi", "i", "am", "om"] // trim() removes leading and trailing spaces. // One-line solution to split into words.