【问题标题】:handle textarea strings in java在java中处理textarea字符串
【发布时间】:2013-09-19 00:55:50
【问题描述】:

如何在 java 中处理多行 textarea 字符串。 这里是我的字符串示例:

eg 1:
!1234   //here after !1234 there is some white space
ADFGKLJUYGHH
eg 2:
!123455//here after!1234555 there  is no space
ASDFGERTYUJDCVBNMFGH

我知道在从文件读取时如何处理这些字符串,但现在我已经从 JTextArea 读取并为这些读取的字符串执行了一些功能。(这里我们应该只考虑字符串的第二行,第一行应该被忽略)

怎么做?

从文件或控制台读取的代码是:

line=br.readLine();
    while (line!=null) 
                            {
                                if (line.length() != 0 && line.charAt(0) == '>') 
                                {
                                    String name = line.trim();
                                    System.out.println(name);   

                                    StringBuffer sb = new StringBuffer();
                                    line = br.readLine();
                                    while(line!=null &&line.length()==0)
                                    {
                                        line = br.readLine();
                                    }
                                    while (line!=null &&line.charAt(0) != '>') 
                                    {
                                        sb.append(line);
                                        if (line!=null) 
                                        {
                                            line = br.readLine();
                                        } 
                                        else 
                                        {
                                            break;
                                        }
                                    }
                                    String ss = sb.toString();

                                    operation(seq,name,fnew,num);
                                } 
                                else 
                                {
                                    line = br.readLine();

                                }
                            }
                    //      br.close();
                        //  bufferFileWriter.close();
                    } 

【问题讨论】:

  • 到目前为止你尝试过什么?看看JTextArea docs,你会发现一个方法会返回它的文本
  • 文本区域中的String 将被每行换行符分隔。对此进行拆分,您将拥有单独的行...按正常方式处理...
  • 如何从文本文件中读取相同的字符串?

标签: java multiline multilinestring


【解决方案1】:

这将拆分大多数新行

String lines[] = theString.split("\\r?\\n|\\r);

但是,这应该足够了

String lines[] = theString.split("\\n");

【讨论】:

  • 好的,谢谢。因为 textarea 将被换行符打破,然后我们可以将字符串拆分为字符串数组
【解决方案2】:

示例代码是我的问题

JTextArea txtarea=new JTextArea(5,30);                    
    String str=txtarea.getText();
                        String[] temp;
                        String delimiter = "\\n";
                        temp = str.split(delimiter);
                      for(int i =0; i < temp.length ; i++)
                        System.out.println(temp[i]);

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 2013-12-19
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多