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]

> 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...
Have a look at String's split(...) method.
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());
}
> Or Use StringTokenizer
can you tell me the url where i'll get detailed tutorial of StringTokenizer
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
> 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+");
> ...
> String regEx = "(\\b[a-z]\\b && [^\\s{1,}+])"
> ...
?
Simply split is on one or more white space characters! (check my previous reply)
> ...
> can you tell me the url where i'll get detailed
> tutorial of StringTokenizer
I bet Google can help you there.
ya u r right prometheuzzActually no need to use Matcher and Patteren
It is simpler to use split . cool
Thank you prometheuzz......
Zorama at 2007-7-29 17:34:37 >

Thank you prometheuzz
Zorama at 2007-7-29 17:34:37 >

str.replace(" ",",");
use the replace function of string perform opration on string using Java string function
Thank you prometheuzz
Zorama at 2007-7-29 17:34:37 >

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 >

> 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]);
}
}
}
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 >

> 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.