8 Replies - 4697 Views - Last Post: 20 November 2010 - 05:05 PM Rate Topic: -----

#1 Guest_Min*


Reputation:

best way to divide a text string into array of words

Posted 20 November 2010 - 03:04 PM

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?

Is This A Good Question/Topic? 0

Replies To: best way to divide a text string into array of words

#2 fender422  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 08-October 10

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"
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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.
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.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: best way to divide a text string into array of words

Posted 20 November 2010 - 03:48 PM

String[] strArray = str.split(" ");
Was This Post Helpful? 1
  • +
  • -

#6 fender422  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 08-October 10

Re: best way to divide a text string into array of words

Posted 20 November 2010 - 04:26 PM

View Postpbl, on 20 November 2010 - 02:48 PM, said:

String[] strArray = str.split(" ");


I guess that's the best way to go, but is there a manual way of doing the same thing without using split()?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: best way to divide a text string into array of words

Posted 20 November 2010 - 04:40 PM

View Postfender422, on 20 November 2010 - 05:26 PM, said:

View Postpbl, on 20 November 2010 - 02:48 PM, said:

String[] strArray = str.split(" ");


I guess that's the best way to go, but is there a manual way of doing the same thing without using split()?

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 ""
Was This Post Helpful? 0
  • +
  • -

#8 fender422  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 08-October 10

Re: best way to divide a text string into array of words

Posted 20 November 2010 - 05:03 PM

View Postpbl, on 20 November 2010 - 03:40 PM, said:

View Postfender422, on 20 November 2010 - 05:26 PM, said:

View Postpbl, on 20 November 2010 - 02:48 PM, said:

String[] strArray = str.split(" ");


I guess that's the best way to go, but is there a manual way of doing the same thing without using split()?

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!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: best way to divide a text string into array of words

Posted 20 November 2010 - 05:05 PM

View Postfender422, on 20 November 2010 - 06:03 PM, said:

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!

Post your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1