• Post Reply Bookmark Topic Watch Topic
  • New Topic

finding string in list with partial contents  RSS feed

 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a List (or array) of strings. {"some string 1", "some string 2","fred smokes","tim drives", "bob flys", "mary drives"};

I want to know if any of the individual strings contains 'drives', and if so how many/which ones.

I 'think' I have to make my own subclass.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam detweiler wrote:I 'think' I have to make my own subclass.

This is not where you should start thinking.

you start by thinking in English (or any other natural language you like). Write out the steps you would take if you had pencil and paper only.

1) Look at each string one at a time
2) see if it contains the word "fred"
3) If so, print it

Then you revise the steps, pretending you are telling a 10 year old child how to do it. refine each step, making them simpler and more granular.

Only after you have done that should you think about writing any java code.
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.


yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam detweiler wrote:
Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.


yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.



How about using a loop along with the previous suggestion?

Henry
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
sam detweiler wrote:I 'think' I have to make my own subclass.

This is not where you should start thinking.

you start by thinking in English (or any other natural language you like). Write out the steps you would take if you had pencil and paper only.

1) Look at each string one at a time
2) see if it contains the word "fred"
3) If so, print it

Then you revise the steps, pretending you are telling a 10 year old child how to do it. refine each step, making them simpler and more granular.

Only after you have done that should you think about writing any java code.


thank you.. but I did not expose my thinking process. which may have included may more steps than you have outlined.

my conclusion remains, unless someone says.. use method foo().. that I will need to make some additional coding of some sort or another.
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
sam detweiler wrote:
Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.


yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.



How about using a loop along with the previous suggestion?

Henry


yes.. I ended up writing my own routines.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam detweiler wrote:
my conclusion remains, unless someone says.. use method foo().. that I will need to make some additional coding of some sort or another.


In my post, I pretty much said to use method foo() (read it again carefully).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not mean for that to be the end of the process...that was the fist step in coming up with "many more steps".

The idea is you think about WHAT needs to be done first. Once you know that, then you start thinking about HOW to do it.

By thinking through what needs to be done, you may realize that one of the things you need to do is look at each and every string in your array or list by itself. So then you think about how you pull them out one at a time - the loop Henry mentions.

Then you have a String...now you need to look at how you check to see if a given String has a specific sub-string. You should realize that it doesn't matter how you got the initial String, so you can focus on just the searching part.

you work through it a piece at a time, and only code a piece at a time. You don't try and do it all in one go.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!