File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Handling Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Handling Strings" Watch "Handling Strings" New topic

Handling Strings

James Robert

Joined: Oct 15, 2004
Posts: 3
Hi All,

I have a set of forbidden and special words placed in a properties file . then when user enters a String , i need to compare the given string with the existing list from properties file and evaluate it . for that i need to check for blank spaces and special characters and white spaces . can you advise me how to do this ?like which is the best option?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Why do it that way? Wouldn't it be easier to read your forbidden words into an array of Strings, then cycle through the array, testing the presence of each forbidden word with String's indexOf method?

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...)
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
refer to this for more help:
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
(I used _ for blank here to make things format beter ...)

You may be asking how to find a word and not a word fragment. You'd want to include "_word." and "_word\n" and "(word)" and maybe "_word's_" but not "wordsmith" or "sword". Is that right?

You might try translating all non-alpha characters to blanks in your text, then use plain indexOf for "_word_". Add an extra blank to the beginning and end of the text just for good measure. Look at String.translateAll() with a regular expression (see Pattern javadoc) that matches all non-alpha. Translate the whole text to lowercase, too.
[ October 20, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Stan -

That's a good technique, very flexible. Sometimes I forget that Java supports regex and that I don't have to resort to Perl to use them!
I agree. Here's the link:
subject: Handling Strings
jQuery in Action, 3rd edition