• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 "-".
 
Greenhorn
Posts: 6
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For that you can use StringTokenizer :
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to split it into 3 different strings as shown above?
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
string[0] = "abc"
string[1] = "def - ghi - jkl"
string[2] = "mno"
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh jim thank you so much.I could not try your method at first.It worked .thanks a lot
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

>
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lookbehind etc., is too difficult for “beginning,”, so I shall move it.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic