permaculture playing cards*
The moose likes Java in General and the fly likes how to check for specific string pattern(using jdk 131) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to check for specific string pattern(using jdk 131)" Watch "how to check for specific string pattern(using jdk 131)" New topic
Author

how to check for specific string pattern(using jdk 131)

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
If I need to check for different ways 'P O BOX' appears in a string how can i do that( i need to see if P appears before O and O appears before Box)etc.

Can I do that easily in jdk 131??


Kishore
SCJP, blog
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You can achieve this in JDK 1.3.1 using charAt(int) and looping through your string.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Nigel Browne:
You can achieve this in JDK 1.3.1 using charAt(int) and looping through your string.


Instead of charat, I can do a index of for P,O and BOX and see if there is a index and the index fall in that order.

My question is, is there a pattern matching function in java.util which can do this in one single api call???
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Pattern matching got added to java in the 1.4 version specifically in the java.util.regex package
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Kishore Dandu:



My question is, is there a pattern matching function in java.util which can do this in one single api call???



yeah a single api can get you the result if you use jdk 1.4 (java.uti.regex)

You may try this..

Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
You could also copy the string to a string buffer and delete all the non-alpha characters then check for POBox using indexOf. Without regex, there's no simple way to do it. Another possibility is to construct an array of strings with each element containing one possible permutation of "PO Box", then use indexOf from either String or StringBuffer to see if it's in the string. An advantage to this is that you could use a parallel array to hold the index of the occurrance, so you could tell which usage exists and where it is in the original test string.

Are you constrained to 1.3 because of project or customer requirements? If not, you may want to upgrade and use regex.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
if constrained by the jdk version, you might alo want to consider jakarta.apache.org's ORO and regexp libraries.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
I am constrained to 1.3.1 for the time being due to technology choices at work.

Here is the solution I am using.

1)Check for index of p, if it is there check for index of o
2)if o index is more than that of p; check for index of box
3) if box index is morethan index of o index(it happens only in case there are two os in the string) then there is PO BOX in the string.

What restricts me is I am filter some address if it is 'Point box road' since it has 'po box' in it
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Kishore -

I think my String array method would eliminate the error you're seeing. Can't hurt to try.

Good luck!

Cheers,

Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check for specific string pattern(using jdk 131)
 
Similar Threads
Deserializing Visual Basic 6 files in Java?
Hour 17:Creating Interactive Web Programs - Java in 24 hours
how can i pass the boolean data type from my java app to oracle function
how to check if a String contains another String
Replacing characters in a string?