Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Tyler Anyan
Tyler Anyan
2,976 Points

What about punctuation?

I was just curious is there a way to take punctuation into consideration or perhaps a method that accounts for that? For example, if I type in DOrK into the program it censors me and exits as desired. If I type in "dork!" though, the exclamation point gets past the censor and the program continues.

2 Answers

Ricky Catron
Ricky Catron
13,023 Points

You could use .contains on the string to check if what you are looking for is in the string at all.

This would catch this like dork! and heyyoudork but miss things like d.o.r.k

Goodluck! --Ricky

Sorry to revive this topic, but why would it not catch d.o.r.k? Isn't that a part of the string?

Probably not, the periods would catch it out. A regular expression could be built I think to ignore symbols. Or add a function that removes special symbols/numbers from any word entries so you are only left with the actual word they wrote.

Ricky Catron
Ricky Catron
13,023 Points

John Magee is correct. If you removed the punctuation with a function or regular expression it would work, but you have to remember the computer sees the symbols differently then you. To you d.o.r.k looks different then dpojrak but the computer sees them as both strings of 8 characters. "dork" is not part of the string "d.o.r.k" because the letters are not next to each other. "dork." has dork in it but "dor.k" does not. It has "dor" and "." and "k".

Goodluck! --Ricky

To Ricky's point - there's only so much filtering you can do and people will find their way around it if they really want to. If you must have 100% filtering - you will have to manually approve every single user input...which isn't practical (for instance, are you going to filter frack thanks to battlestar, or british words like arse and bollox?)

Thanks guys.

Regular Expressions, though a giant pain to learn and implement, can be your friend in situations like this. You'd just have to write a regular expression that scanned for 'dork' (case insensitive) most likely at the beginning of a word.

Language filters are tough