File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling Strings

 
James Robert
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
refer to this for more help:

http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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 ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic