开发者

Java code problem: search and replace

Have a question: how can I in Java find all the words in the texts that are, for example 4 symbols long? and then how can I replace them?

for finding some piece of text I wrote

<

fButton.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{int i=text.getText().indexOf(field2.getText(),text.getSelectionStart());
if(i>=0)
text.select(i,i+field2.getText().length());
text.requestFocusInWindow()

;>

I tried to combine buttons and fields, but all I have are 2 errors:

symbol  : method parseInt (javax.swing.JTextField)
location: class java.lang.Integer
numb=Integer.parseInt(field2);
            ^
symbol  : method replaceAll (java.lang.String,javax.swing.JTextField)
location: class javax.swing.JTextArea
text = text.replaceAll("\\b\\w{4}\\b", field3);
           ^

code:

 field2=new JTextField("Enter number",10);
    int numb;
    numb=Integer.parseInt(field2);
    tPanel.add(field2);
    JButton fButton=new JButton ("Find and Replace");
    tPanel.add(fButton);
    fButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    text = text.replaceAll("\\b\\w{4}\\b", field3);
    }});
    field3=new JTextFiel开发者_JS百科d("Enter replace",10);
    tPanel.add(field3);

Cant understand it(((


You wrote "symbols" in your question. I will intepreat that as "word characters". If you would like another definition look at the regular expression summary in the Pattern api.

Use String.replaceAll(regularExpression, replacementText) and the regular expression to use is "\b\w{4}\b".

  • \b is "word boundary"
  • \w is "word characters"
  • {4} is how many of the "before" symbols to match (in this case word characters)

So to replace all 4-letter words in a string text do (note that all \'s are escaped by another \):

text = text.replaceAll("\\b\\w{4}\\b", "replaced");

For example:

System.out.println("bla blaa hello test".replaceAll("\\b\\w{4}\\b", "mupp"));

Outputs:

bla mupp hello mupp

To answer your updated question:

Code below produced this screeshot:

Java code problem: search and replace

Code:

public static void main(String[] args) {

    final JTextArea area = new JTextArea("bla blaa hello test");
    final JTextField numberField  = new JTextField("4", 10);
    final JTextField replaceField = new JTextField("muff", 10);

    JButton button = new JButton(new AbstractAction("Replace") {
        public void actionPerformed(ActionEvent event) {
            String text = area.getText();
            int    syms = Integer.parseInt(numberField.getText());
            String repl = replaceField.getText();

            area.setText(text.replaceAll("\\b\\w{"+syms+"}\\b", repl));
        }
    });

    JPanel panel = new JPanel();
    panel.add(numberField);
    panel.add(replaceField);
    panel.add(button);

    JFrame frame = new JFrame("Test");
    frame.add(area, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜