jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes Help required regarding splitting string in words in java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Help required regarding splitting string in words in java" Watch "Help required regarding splitting string in words in java" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Help required regarding splitting string in words in java
 
Similar Threads
delimeters and regex
Is this group active
Splitting a string in 2 and turning it into hexadecimcal
Need help with regexp
Splitting a string to smaller strings.