• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Handling Strings

 
Greenhorn
Posts: 3
  • 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?
 
Ranch Hand
Posts: 805
  • 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?
 
Ranch Hand
Posts: 233
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • 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
  • 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
Boost this thread!