简体   繁体   English

如何在DocumenListener中读取最后一个单词或最后一行

[英]How to read last word or last line in DocumenListener

I implement DocumentListener for JEditorPane ( need help with method insertUpdate(DocumentEvent e) ). 我为JEditorPane实现DocumentListener(需要有关方法insertUpdate(DocumentEvent e)的帮助)。 How to read last word or last line (lines are separated by '\\n' and words are separated by ' ' ) ? 如何阅读最后一个单词或最后一行(行之间用'\\ n'分隔,单词之间用''分隔)?

Utilities class can be used with formatted content eg html 实用程序类可与格式化内容一起使用,例如html

public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException 
public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException 

To get the last line in your JEditorPane, split the text in the editor on \\n as shown below: 要获得JEditorPane中的最后一行,请在编辑器中的\\n上拆分文本,如下所示:

String text = editor.getText();
String[] lines = text.split("\n");
String lastLine = lines[lines.length-1];
System.out.println("Last line: " + lastLine);

Similarly, to get the last word, split the last line on space. 同样,要获取最后一个单词,请在空间上拆分最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM