int numberOfWords = sentence.trim().split(" ").length;
Hey everyone. I was just reading through some posts and I saw this as a way to count words in a sentence. I then went into the string section of the API to try to make sense of it. I understand that "trim" gets rid of the white space at the beginning and end and that "split" along with the given parameter splits the string each time there is a space. However, I dont get how these combined with length gives the number of words. Thanks! :)
EDIT: Wow it just hit me. I feel like a complete idiot. Disregard my question but thanks anyway! :)
Thank you. Separating it does make it quite a bit clearer. Your reply gave me another question though :p. Do you have to make your string into an array of words when you split it (I'm assuming that's what it's doing. We haven't covered arrays in my class yet so I'm not very familiar with them).