aspose file tools
The moose likes Beginning Java and the fly likes Help with String conversions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Help with String conversions" Watch "Help with String conversions" New topic
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
    
    6

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Help with String conversions
 
Similar Threads
Parsing using Java
My Vista has an attitude...
Printing Diamon in java
Loop With Switch
removing newline and tabs from a string