Using Arrays to Manage Text

There are no built-in string Methods that directly allow you to break text strings up into words or sentences, but there is a string Method that indirectly makes this very easy to do. It's called the split() Method.

The split() Method

The split() Method is a string Method that returns an Array. It takes any character as an argument, and the array that it returns is made up of separate chunks of the text string divided into array items at all the points where that character appeared. NOTE that the split() Method removes the dividing character from the text string and does not put it into the array that it returns.

That means that if you wanted to analyze the words in a sentence, you could use the space character as the dividing character (often called an "item delimiter"). It works like this.

text = "This is a sample sentence."
wordArray = text.split(" ")

Above example in "action"

By placing a blank space in the parentheses after the text.split(), the array that it returns is divided by spaces, which means each word in the sentence occupies its own position in the array where it can be easily accessed by number.

For instance, in this case wordArray[0] would equal "This", and wordArray[1] would equal "is", etc.

Using this technique, you could also divide a paragraph into sentences by using the "." character as an item delimiter.