This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "going through a string" Watch "going through a string" New topic

going through a string

Joe Panully

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.


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.


Impossible is I M Possible
I agree. Here's the link:
subject: going through a string
It's not a secret anymore!