| Author |
Help required regarding splitting string in words in java
|
adil ibnashraf
Greenhorn
Joined: Aug 27, 2010
Posts: 10
|
|
Hi ,
I need to split a given string into words respectively, The string can be like "Hello Code Ranch ...!! Its So good to be here"
So i want it to be split as
Hello
Code
Ranch
Its
good
to
be
here
I am using String.split("[^a-zA-Z]") for the same
but getting output as
Hello
Code
Ranch
its
good
to
be
here
Can any one help out ?
|
Regards
Adil Hussain
- I want to speak Java
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Hi and welcome to the JavaRanch.
You're splitting the string on everything that is not a letter. And because you have ...!! in the sentence you got a lot of empty strings. What you want is that it takes the whole group (...!!) and split on that. You can achieve that by adding a quantifier.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
adil ibnashraf
Greenhorn
Joined: Aug 27, 2010
Posts: 10
|
|
|
Can you please give me a regular expression having quantifier and serving my purpose
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
First read the article and try some of the quantifiers. If I just tell you the solution you'll learn nothing.
|
 |
 |
|
|
subject: Help required regarding splitting string in words in java
|
|
|