[英]How to read last word or last line in DocumenListener
我為JEditorPane實現DocumentListener(需要有關方法insertUpdate(DocumentEvent e)的幫助)。 如何閱讀最后一個單詞或最后一行(行之間用'\\ n'分隔,單詞之間用''分隔)?
實用程序類可與格式化內容一起使用,例如html
public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException
public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException
要獲得JEditorPane中的最后一行,請在編輯器中的\\n
上拆分文本,如下所示:
String text = editor.getText();
String[] lines = text.split("\n");
String lastLine = lines[lines.length-1];
System.out.println("Last line: " + lastLine);
同樣,要獲取最后一個單詞,請在空間上拆分最后一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.