How to Remove Punctuation in a Java String

Updated July 20, 2017

Students of programming are often given interesting and educational challenges, such as removing all the punctuation in a string of text. This particular task requires the knowledge of string handling for a particular language. The Java language has robust string-handling facilities that make a task like this fairly simple, but also informative if you are new to Java. You can remove the punctuation from a string in Java with just a few lines of code.

Load the NetBeans Java development environment by clicking on its icon. Choose "File/New Project" from the menu at the top of the page. Select "Java Application" to create a new source code file. The source code file only contains a main function, which is where you will put all your code.

Create a string with a lot of unnecessary punctuation, like this:

Remove all of the punctuation by using a regular expression like this:

Print out the string like this:

Press F6 to launch the program. The output looks like this:


Things You'll Need

  • Java Development Kit
  • Netbeans Bundle
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Mike Wallace began writing professionally in 2009. He is currently employed as a software engineer who designs, develops and tests software systems. He holds a Bachelor of Science in computer engineering and a Master of Science in electrical and computer engineering from California State University, Chico.