How to Remove Punctuation in a Java String

Written by mike wallace
  • Share
  • Tweet
  • Share
  • Email

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.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Java Development Kit
  • Netbeans Bundle

Show MoreHide


  1. 1

    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.

  2. 2

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

        String str = "Punctuation: Removal; String.";
  3. 3

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

        str = str.replaceAll("[^A-Za-z0-9]", "");
  4. 4

    Print out the string like this:

  5. 5

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


Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.