Swing/GetLineText のバックアップソース(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/GetLineText へ行く。
- 1 (2006-10-10 (火) 14:36:01)
- 2 (2006-11-06 (月) 13:06:59)
- 3 (2006-11-10 (金) 13:50:48)
- 4 (2007-05-07 (月) 13:55:23)
- 5 (2007-05-27 (日) 01:31:30)
- 6 (2007-06-04 (月) 15:08:35)
- 7 (2007-12-10 (月) 20:48:52)
- 8 (2012-02-09 (木) 12:16:59)
- 9 (2012-06-19 (火) 14:55:23)
- 10 (2013-02-22 (金) 19:02:46)
- 11 (2014-12-18 (木) 17:17:10)
- 12 (2015-10-15 (木) 00:18:22)
- 13 (2016-05-30 (月) 16:27:12)
- 14 (2016-09-17 (土) 20:53:53)
- 15 (2017-10-26 (木) 10:59:52)
- 16 (2019-04-11 (木) 13:24:20)
- 17 (2021-01-23 (土) 21:35:26)
- 18 (2023-07-07 (金) 13:57:47)
TITLE:JTextAreaから一行ずつ文字列を取得 #navi(../) #tags() RIGHT:Posted by &author(aterai); at 2006-10-09 *JTextAreaから一行ずつ文字列を取得 [#sce4576b] JTextAreaなどのテキストコンポーネントから一行ずつ文字列を取り出してそれを処理します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTNWn74dWI/AAAAAAAAAao/pNdeF8CSOfM/s800/GetLineText.png) **サンプルコード [#cba0b2ee] #code(link){{ int count = 0; StringTokenizer st = new StringTokenizer(textArea.getText(), "\n") ; while(st.hasMoreTokens()) { if(st.nextToken().startsWith("#")) { count++; } } }} **解説 [#v1446cab] 上記のサンプルでは、JTextArea#getText() ですべてのテキストを取得し、StringTokenizer を使って行毎に分解しています。 ---- - String#split #code{{ for(String line: textArea.getText().split("\\n")) { if(line.startsWith("#")) { count++; } } }} - LineNumberReader #code{{ try(LineNumberReader lnr = new LineNumberReader(new StringReader(textArea.getText()))) { String line = null; while((line = lnr.readLine()) != null) { if(line.startsWith("#")) { count++; } } }catch(IOException ioe) { ioe.printStackTrace(); } }} - Element#getElementCount #code{{ Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); try{ for(int i=0;i<root.getElementCount();i++) { Element elem = root.getElement(i); String line = doc.getText(elem.getStartOffset(), elem.getEndOffset()-elem.getStartOffset()); if(line.startsWith("#")) { count++; } } }catch(BadLocationException ble) { ble.printStackTrace(); } }} //**参考リンク **コメント [#ee4fa454] #comment