my dog learned polymorphism*
The moose likes Java in General and the fly likes how to check if a String contains another String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to check if a String contains another String" Watch "how to check if a String contains another String" New topic
Author

how to check if a String contains another String

Adrian Airloy
Greenhorn

Joined: Dec 31, 2002
Posts: 11
I need to check if the value of one String is contained in another. Consider the following-
String strAddress = "123 Main street, P.O Box 456";
String strInvalid = "P.O Box";
I need to compare the two to determine if strAddress contains strInvalid anywhere in it.
Note: in reality, i would have to check for multiple formats of strInvalid Strings. e.g. "P.O Box" , "PO.Box" , "P O Box"...etc
Would appreciate any help/advice in getting this done.
Thanks :roll:


SCJP.<br />I haven't lost my mind, it's backed up on a disk somewhere!
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Hi um .. Jumpin'.
(You want to change that before the sheriff gets after you.)
As for your problem, it sounds like you wouldn't be able to use any useful String methods unless you checked each one. So, I would say your next bet is the use of regular expressions. Have you looked into that?


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Adrian Airloy
Greenhorn

Joined: Dec 31, 2002
Posts: 11
Hey Nathaniel Stoddard

(You want to change that before the sheriff gets after you.)

... IIIII shot the sheeeeriffff .... but i did not kill the....
use of regular expressions

Nope have not done that yet, but thanks for the tip and the quick response.
If you have anything specific i should investigate, please let me know.'
Thanks again.
[ April 22, 2004: Message edited by: Jumpin' Java ]
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
the Sheriffs 'round here are terrible creatures, and very mean, and they have big teeth, and they eat babies.
The bartenders are very cool, OTOH. Very cool.
However, we both need to enforce a naming policy, so please come up with a plausible (non-fictional-sounding, non-famous) first name and last name, and I'll buy you a beer.
In the meantime, do a search on regular expressions on this forum, and you'll get what you want. As a quick start, try the following.


HTH,
M
[ April 22, 2004: Message edited by: Max Habibi ]
[ April 22, 2004: Message edited by: Max Habibi ]

Java Regular Expressions
 
 
subject: how to check if a String contains another String
 
Similar Threads
Form does not validated correctly
Searching for a Design Pattern
parse String
selecting a check box from a list of check boxes
Forcing a JComboBox to stay open