• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with String conversions  RSS feed

 
Syed Saulat Rizvi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!