Retrieving the words of a sentence

Sir,

I have a

String holdme="Sun Shine Moon Light";

I need to break the String into different words like, Sun, Shine, Moon, Light so that to store each word of the string in an aray. What should I do ?

please help me..

[276 byte] By [Zorama] at [2007-11-27 11:40:54]
# 1

> Sir,

madam,

>What should I do ?

you should look in the api docs for class String to see if just maybe it has a method that does exactly what you want. maybe it's not called exactly "break" but something equally descriptive...

OnBringera at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 2

Have a look at String's split(...) method.

prometheuzza at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 3

Or Use StringTokenizer

shagila at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 4

use regex in java like this

[code]

String splitString = "Sun moon earth water";

String regexp = "(\\b[sun]\\b)"

Matcher m = Pattern.compile( regexp ).matcher( sourceSegment );

while (m.find())

{

System.out.println("String found " +m.group.toString());

}

AmitChalwade123456a at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 5

> Or Use StringTokenizer

can you tell me the url where i'll get detailed tutorial of StringTokenizer

Sumadurama at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 6

U need to modify regEx.

this may help u

String regEx = "(\\b[a-z]\\b && [^\\s{1,}+])"

put this in regex in previous code . I think it will work

Message was edited by:

AmitChalwade123456

AmitChalwade123456a at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 7

> use regex in java like this

>

> String splitString = "Sun moon earth water";

> String regexp = "(\\b[sun]\\b)"

1 - that regex is completely wrong: it splits on a word boundary, followed by either s, u or n, followed by another word boundary;

2 - why use find() and group() over a simple split(...)?

@OP: just try this:

String holdme = "Sun Shine Moon Light";

String[] words = holdme.split("\\s+");

prometheuzza at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 8

> ...

> String regEx = "(\\b[a-z]\\b && [^\\s{1,}+])"

> ...

?

Simply split is on one or more white space characters! (check my previous reply)

prometheuzza at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 9

> ...

> can you tell me the url where i'll get detailed

> tutorial of StringTokenizer

I bet Google can help you there.

prometheuzza at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 10

ya u r right prometheuzzActually no need to use Matcher and Patteren

It is simpler to use split . cool

AmitChalwade123456a at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 11

Thank you prometheuzz......

Zorama at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 12

Thank you prometheuzz

Zorama at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 13

str.replace(" ",",");

use the replace function of string perform opration on string using Java string function

sandeep_2012a at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 14

Thank you prometheuzz

Zorama at 2007-7-29 17:34:37 > top of Java-index,Java Essentials,Java Programming...
# 15

Can't I try this?..

instead of

String holdme = "Sun Shine Moon Light";

String[] words = holdme.split("\\s+");

can i use

String holdme = "Sun Shine Moon Light";

String[] words = holdme.split(" ");

please suggest

Zorama at 2007-7-29 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 16

> Can't I try this?..

> instead of

> String holdme = "Sun Shine Moon Light";

> String[] words = holdme.split("\\s+");

>

> can i use

>

> String holdme = "Sun Shine Moon Light";

> String[] words = holdme.split(" ");

>

> please suggest

Yes, you can. But there is a difference:

public class Foo {

public static void main(String[] args) {

String holdme = "SunShineMoon Light";

String[] words = holdme.split(" ");

for(int i = 0; i < words.length; i++) {

System.out.println("words["+i+"] = "+words[i]);

}

words = holdme.split("\\s+");

for(int i = 0; i < words.length; i++) {

System.out.print("\nwords["+i+"] = "+words[i]);

}

}

}

prometheuzza at 2007-7-29 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 17

Oh Thank you so much Sir,

Your sample program is so helpful, It open up my idea, I learnt a new logic..

Thank you so much..

Zorama at 2007-7-29 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 18

> Oh Thank you so much Sir,

> Your sample program is so helpful, It open up my

> idea, I learnt a new logic..

> Thank you so much..

; )

No problem.

prometheuzza at 2007-7-29 17:34:42 > top of Java-index,Java Essentials,Java Programming...