public String censorString(String originalString)
{
StringBuffer orig = new StringBuffer(originalString);
Pattern p = Pattern.compile("[0-9/A-Z/a-z]+");
Matcher m = p.matcher(orig);
StringBuffer censor = new StringBuffer();
boolean result = m.find();
while (result)
{
String match = originalString.substring(m.start(), m.end());
if(match.equals("badword"))
m.appendReplacement(censor, "[censored]");
result = m.find();
}
m.appendTail(censor);
return censor.toString();
}
Function To Censor String
Here is a function that censors a string, but leaves in all of the punctuation. It searches for strings of letters and numbers and if it matches "badword", will replace it with "[censored]".
find similar posts:
Java,
Patterns,
Regular Expressions
My favorite quotations..
“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.” by Robert A. Heinlein
"We are but habits and memories we chose to carry along." ~ Uki D. Lucas
Recommended pages
Popular Recent Articles
-
I have noticed a very unsettling statistic on my blog. This prompted a fascinating question about AI, blogs' future, and maybe even the...
-
O'REILLY 201 0011 031 10110100180 000110111 01100041 001100010010000 5011011001010 1101110011 000100000 00000 10 1000012 Escaping the Bu...
-
Installation of Java on Pi is easy, you can ssh to your Pi remotely and just execute: pi@raspberrypi ~ $ sudo apt-get update && su...
-
Epiphany is one of these interesting words that can mean so much. For me it means the crossroad where I chose the road less travelled. The r...
-
"Learn how to write, story-tell, or give up, you will not make a mark otherwise" ~ Greg Isenberg View on Amazon book Storyworthy:...
-
On the mobile, the page is hard to read so I am including a LINK to Google document ...
-
How to Win Friends and Influence People: Updated For the Next Generation of Leaders by Dale Carnegie "The Only Book You Need to Lead Y...
-
At some point, each application will have to perform some logic based on different cases. extract your CONSTANTS extract your logic into a s...
-
I progressively cut my hair shorter and shorter. Now, I just came back from the swimming pool with Lili, so it is a mess.