Java - How To Remove Special Characters From Text In Java

How To Remove Special Characters From a String In Java Netbeans

How To Remove Special Characters From Text In Java


In this Java Tutorial we will see How To Make a simple graphical tool for users to input text and remove special characters from the entered text with the click of a button In Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author 1BestCsharp
 */
public class RemoveSpecialCharactersForm extends JFrame {

    private JTextArea textArea;
    private JButton removeButton;
    
    public RemoveSpecialCharactersForm(){
        setTitle("Remove Special Characters Form");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null);
        
        initializeGUI();
    }
    
    private void initializeGUI(){
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setFont(new Font("Arial", Font.BOLD, 45));
        textArea.setText("@#RandomT3xtW!th$pec!@lCh@ract3r$123!@#$%^&*()-=_+[]{}|;:'\",.<>?/");
        
        JScrollPane scrollPane = new JScrollPane(textArea);
        
        removeButton = new JButton("Remove Special Characters");
        removeButton.setFocusPainted(false);
        removeButton.setBorderPainted(false);
        removeButton.setForeground(Color.white);
        removeButton.setBackground(new Color(50,10,20));
        removeButton.setFont(new Font("Arial", Font.BOLD, 16));
        
        removeButton.addActionListener((e) -> {
           
            removeSpecialCharacters();
            
        });
        
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(removeButton, BorderLayout.SOUTH);
        add(panel);
    }
    
    
    private void removeSpecialCharacters(){
        try
        {
            String originalText = textArea.getText();
            // Remove any characters that are not alphanumeric, comma, period,  
            // single quote, double quote, colon, or whitespace
            String sanitizedText = originalText.replaceAll("[^a-zA-Z0-9,'\":\\s]+", "");
        
            textArea.setText(sanitizedText);
        }
        catch(Exception e){ System.out.println(e.getMessage()); }
    }
    
    public static void main(String[] args) {
        new RemoveSpecialCharactersForm().setVisible(true);
    }
    
}



The Final Result:

How to Create a Remove Special Characters Form Using Java NetBeans

How to Create a From to Clear a Text From Any Special Characters In Java Netbeans










Share this

Related Posts

Previous
Next Post »