wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes going through a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "going through a string" Watch "going through a string" New topic
Author

going through a string

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
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

Joined: May 15, 2001
Posts: 673
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
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


Impossible is I M Possible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: going through a string