Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15754
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 "-".
 
Devarsh Shah
Greenhorn
Posts: 6
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that you can use StringTokenizer :
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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:

>
 
Campbell Ritchie
Marshal
Posts: 53126
124
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookbehind etc., is too difficult for “beginning,”, so I shall move it.
 
Henry Wong
author
Sheriff
Posts: 22610
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!