What would be the best way (in terms of efficiency) to divide a text string into array of words?
for example, if the user types "peter griffin is irish", I want to create an array of 4 words, so stringArray[0] would be "peter".
I was thinking about using for-loop to count the number of spaces in the string and adding 1 so that I can get the number of array size needed, but this did not work if I typed "petter griffin is irish", since there are multiple number of spaces dividing the word.
could anyone suggest an idea with actual written code example on how to do it?
8 Replies - 3069 Views - Last Post: 20 November 2010 - 05:05 PM
#1 Guest_Min*
best way to divide a text string into array of words
Posted 20 November 2010 - 03:04 PM
Replies To: best way to divide a text string into array of words
#2
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 03:08 PM
forgot to log-in, and I made a typo.
* but this did not work if I typed "peter griffin is (five spaces) irish", since there are multiple number of spaces dividing the word.
meaning there are five spaces between "is" and "irish"
* but this did not work if I typed "peter griffin is (five spaces) irish", since there are multiple number of spaces dividing the word.
meaning there are five spaces between "is" and "irish"
#3
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 03:23 PM
Just use the String split() method, with " " as the param. It will return a String[] with one word per element.
#4
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 03:32 PM
If you don't want to use String methods, you can always loop as you said, and "create" the words.
here is a pseudo code:
have a String currentWord variable.
you will have to use an ArrayList of String Object as the words array in that case (and not a 'plain' Array) since you don't know how many String objects (words) you might have.
here is a pseudo code:
have a String currentWord variable.
1. loop the sentence
1.1 if the next char is not space, add it to currentWord.
1.2 else (the char is space)
1.2.1 if the currentWord is NOT empty,
1.2.1.1 add it to the words array
1.2.1.2 empty currentWord (currentWord = "")
2. print the words array
you will have to use an ArrayList of String Object as the words array in that case (and not a 'plain' Array) since you don't know how many String objects (words) you might have.
#5
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 03:48 PM
String[] strArray = str.split(" ");
#6
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 04:26 PM
#7
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 04:40 PM
fender422, on 20 November 2010 - 05:26 PM, said:
You can always
char[] digit = str.toCharArray();
for() loop into digit[] to count the ' '
make a String[] array of count of ' ' + 1
repeat your loop and fill a StringBuilder
when you encounter a ' ' or end of the array convert the StringBuilder into a String and reset it to ""
#8
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 05:03 PM
pbl, on 20 November 2010 - 03:40 PM, said:
fender422, on 20 November 2010 - 05:26 PM, said:
You can always
char[] digit = str.toCharArray();
for() loop into digit[] to count the ' '
make a String[] array of count of ' ' + 1
repeat your loop and fill a StringBuilder
when you encounter a ' ' or end of the array convert the StringBuilder into a String and reset it to ""
I understand your method and I tried it, and I successfully divided one space between two spaces, but failed if there were more than one space in between...I think I made a mistake somewhere but I guess I'll just use split(). thank you!
#9
Re: best way to divide a text string into array of words
Posted 20 November 2010 - 05:05 PM
Page 1 of 1
|
|

New Topic/Question
Reply
MultiQuote










|