• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

going through a string

 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am passed a string, say "potato" and i'm running a program that say, if there is a "p" in the string I print some kind of message, how would I check to see if there is a "p" in said string? I know I just need to break up the string and do an if statement and I think it has something to do with stringTokenizers, but i'm not sure how to do it.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are more than one way to achieve what you want. Have a look at the indexOf method of class String in the APIs
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In particular, you should look at the docs for the String class. As Nigel says, the indexOf() method will serve your purposes well, unless you are required to implement this on your own for a class assignment. In that case, you may need to use charAt() instead. I think StringTokenizer will be overkill since String already provides the functionality that you need.

Layne
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

If you just want to traverse the String use this code Hope it will help.



otherwise in general, if you just want to see if there is a 'p' in the String then you can use "indexOf" method.



regards
Prashant
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic