| Author |
Help with String conversions
|
Syed Saulat Rizvi
Greenhorn
Joined: May 05, 2005
Posts: 5
|
|
Hi
I need to convert Strings like 'baseball t shirts' or 'smokless e cigerattes' into 'baseball t-shirts' or 'smokless e-cigerattes' in java, can any body tell me how can i do that.
I dont want to search each character like 't' or 'e' in this example for spaces.
I thought indexOf will do that for me but not
Help will be appreciated
|
 |
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
|
|
|
If this is the format the strings will be in, you can get the last index of the space character and replace that with a hyphen.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
examples are nice, but they don't define the conditions. You need to give explicit rules on when you need to insert a hyphen and when you don't. Once you define the rules, coding is easy.
So, what are the rules? is any 't' character followed by a space, followed by a word to be combined? same with 'e' followed by space? Does the case of any of the letters make a difference? What if there are multiple spaces?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Check out java.util.regex.Pattern in combination with java.util.regex.Matcher. In short:
- create a Pattern with the right pattern
- get a Matcher from the pattern for the input string
- use the matcher to search once (hint: the method is not called search but still quite similar)
- use the start of the match as the index of the found match
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Help with String conversions
|
|
|