How can I split a string into words in Java without using String.split()?

java, string

Solution

I believe that your teacher is asking you to process the string yourself (without using any other libraries to do it for you). Check to see if this is the case - if you can use them, there are things such as StringTokenizer, Pattern, and Scanner to facilitate string processing.

Otherwise...

You will need a list of word separators (such as space, tab, period, etc...) and then walk the array, building a string a character at a time until you hit the word separator. After finding a complete word (you have encountered a word separator character), save it the variable out into your structure (or whatever is required), reset the variable you are building the word in and continue.

Problem

My teacher specifically requested that we split a sentence into words without using `String.split()`. I've done it using a `Vector` (which we haven't learned), a `while`-loop, and substrings. What are other ways of accomplishing this? (preferably without using `Vectors`/`ArrayLists`).

Original source