• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding phrases in a String

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to do assignment:

Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2

I'm not sure exactly how to find/extract the word "code" out of a string. Do I use substring?

Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Substring is one way, but regular expressions would be easier for this. Check out java.util.regex.Pattern and java.util.regex.Matcher.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? I'd probably just use the indexOf methods and loop--easier than learning a mini-language, at least in my book :)
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure I have to use substring because I don't know any other methods.
Thanks for your help anyway!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be able to look up a method.

java.lang.String

And look at the indexOf methods.

(Not sure why that isn't linking to the docs.)
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Really? I'd probably just use the indexOf methods and loop--easier than learning a mini-language, at least in my book :)

I would have used indexOf as well, if one of the requirements wasn't the-third-character-can-be-anything.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, missed that part!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now we have all worked out what the question was, where shall we look for the answer?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic