This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to split Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to split "abc-def - ghi-jkl"?" Watch "how to split "abc-def - ghi-jkl"?" New topic
Author

how to split "abc-def - ghi-jkl"?

antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 43
how to avoid the white spaces while performing the split operation for the string as follows:

"abc-def - ghi-jkl"

I want 3 separate splitted strings likw

string[0] = "abc"
string[1] ="def - ghi"
string[2] = "jkl"
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Can you explain in more detail what exactly you are trying to do and what the exact problem is? The more detailed you explain your problem, the better people can help you.

I'm guessing that you have a string "abc-def - ghi-jkl" and that you want to split it into four strings, "abc", "def", "ghi", "jkl"? You can do that with the split() method of class String, using an appropriate regular expression that matches on "-" with optional spaces in front and behind the "-".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Devarsh Shah
Greenhorn

Joined: Mar 02, 2012
Posts: 6

For that you can use StringTokenizer :
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 43
how to split it into 3 different strings as shown above?
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
antsi klando wrote:how to split it into 3 different strings as shown above?




This is called "Negative Lookahead and Lookbehind".

What you'r basically saying here is: Take all characters that are a - (dash), except the ones that are preceded or followed by a whitespace (\s).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

antsi klando wrote:how to split it into 3 different strings as shown above?

Without knowing more about why it gets split that way, it's difficult to answer. Is it the extra spaces that make the difference?

Do you have any other test examples? For example, if you had:
"abc-def - ghi - jkl-mno-pqr"
how would you want it split then?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 43
string[0] = "abc"
string[1] = "def - ghi - jkl"
string[2] = "mno"
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Jim Pouwels wrote:
antsi klando wrote:how to split it into 3 different strings as shown above?




This is called "Negative Lookahead and Lookbehind".

What you'r basically saying here is: Take all characters that are a - (dash), except the ones that are preceded or followed by a whitespace (\s).


Doesn't this work?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

antsi klando wrote:string[0] = "abc"
string[1] = "def - ghi - jkl"
string[2] = "mno"

Yes, but why?

You need to explain the rules; otherwise we can't help you.

Also: what happened to "pqr"?

Winston
antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 43
also pqr separately
the condition is that every string should be separated except for the pattern "abc - def - gfh -..........".there should not be any white spaces in between the string.Only then the split operation should be performed
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
antsi klando wrote:also pqr separately
the condition is that every string should be separated except for the pattern "abc - def - gfh -..........".there should not be any white spaces in between the string.Only then the split operation should be performed


Oke, for the 3rd time. This should do the trick:

antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 43
Oh jim thank you so much.I could not try your method at first.It worked .thanks a lot
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

antsi klando wrote:Oh jim thank you so much.I could not try your method at first.It worked .thanks a lot

Yes, but do you understand why it works? That's the most important thing - otherwise you'll be back with a similar question next time you have a problem.

Winston
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Winston Gutkowski wrote:
antsi klando wrote:Oh jim thank you so much.I could not try your method at first.It worked .thanks a lot

Yes, but do you understand why it works? That's the most important thing - otherwise you'll be back with a similar question next time you have a problem.

Winston


As I said earlier:

This is called "Negative Lookahead and Lookbehind".

What you'r basically saying here is: Take all characters that are a - (dash), except the ones that are preceded or followed by a whitespace (\s).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Wouldn't you technically need a negative lookbehind and a negative lookahead? Your regex is using two lookbehinds. It may work (haven't tested) but it just looks wrong to me.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Rob Spoor wrote:Wouldn't you technically need a negative lookbehind and a negative lookahead? Your regex is using two lookbehinds. It may work (haven't tested) but it just looks wrong to me.


My mistake:

>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Lookbehind etc., is too difficult for “beginning,”, so I shall move it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Jim Pouwels wrote:
Winston Gutkowski wrote:
antsi klando wrote:Oh jim thank you so much.I could not try your method at first.It worked .thanks a lot

Yes, but do you understand why it works? That's the most important thing - otherwise you'll be back with a similar question next time you have a problem.

Winston


As I said earlier:

This is called "Negative Lookahead and Lookbehind".

What you'r basically saying here is: Take all characters that are a - (dash), except the ones that are preceded or followed by a whitespace (\s).


Jim,

I think you are missing the point of Winston's response.... I believe Winston is pointing out to antsi, to not use any solution which is not understood. This is a flaw of just giving out an answer, particularly for regexes. The user doesn't understand it -- and is very unlikely to be able to fix the issue, if a bug shows up, or if the requirement changes.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to split "abc-def - ghi-jkl"?
 
Similar Threads
How do I refresh a jtable after I delete a row?
Eclipse - Project Naming
Problem in regular expression for matching
problem in writing Regular Expression
Parsing a String value in StoredProcedure???