如何用某个单词替换最后一个单词的文本区域?

时间:2018-10-26 20:28:56

标签: java string jtextarea

我从madprogrammer获得了代码: How to read last word or latest word in JTextArea

但是我需要替换文本区域中的最后一个单词,也许使用文档过滤或在lastword和beforelastword之间使用空格((“”)替换)。

有人可以帮我吗?我在Google中搜索仍然没有找到方法。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Utilities;

public class TheLastWord {

    public static void main(String[] args) {
        new TheLastWord();
    }

    public TheLastWord() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTextArea ta = new JTextArea(10, 20);
            add(new JScrollPane(ta));
            JLabel lastWord = new JLabel("...");
            add(lastWord, BorderLayout.SOUTH);

            ta.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void insertUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                protected void checkLastWord() {
                    try {
                        int start = Utilities.getWordStart(ta, ta.getCaretPosition());
                        int end = Utilities.getWordEnd(ta, ta.getCaretPosition());
                        String text = ta.getDocument().getText(start, end - start);
                        lastWord.setText(text);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用像(\w+.?)$这样的正则表达式,即使它以.结尾,它也将匹配字符串的最后一个单词。

String sentence = "I am a full sentence";

String replaced = sentence.replaceAll("(\\w+.?)$", "replaced");
System.out.println(replaced); // prints 'I am a full replaced'

答案 1 :(得分:0)

您可以分裂。并获取最后一个索引,然后插入像这样的子字符串

    String sentence ="Meow test test hello test.";

    String[] temp = sentence.split("[|!|\\?|.|\\s|\\n]");
    String word = temp[temp.length-1];
    int index = sentence.lastIndexOf(word);
    String out = sentence.substring(0,index) + " INSERTED WORD" + sentence.substring(index+word.length(), sentence.length());