开发者

How to read last word or last line in DocumenListener

开发者 https://www.devze.com 2023-02-25 09:19 出处:网络
I implement DocumentListener for JE开发者_如何学CditorPane ( need help with method insertUpdate(DocumentEvent e) ). How to read last word or last line (lines are separated by \'\\n\' and words are sep

I implement DocumentListener for JE开发者_如何学CditorPane ( need help with method insertUpdate(DocumentEvent e) ). How to read last word or last line (lines are separated by '\n' and words are separated by ' ' ) ?


Utilities class can be used with formatted content e.g. 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:

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.

0

精彩评论

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