0

How to handle a multi line textarea string in java. Here example of my strings:

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

I know how to handle these strings when reading from a file but now i have read from a JTextArea and perform some function for those read strings.(Here we should consider only the second line of a string the first should be ignored)

How to do that?

The code for the reading from a file or from console is:

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();
                    } 
saitds
  • 3
  • 1
  • 6
  • 5
    What have you tried so far? Take a look at the [JTextArea docs](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html), you'll find a method that will return its text – BackSlash Sep 14 '13 at 11:59
  • The `String` from the text area will be broken by a new line character per line. Split on this and you have the individual lines...process as per normal... – MadProgrammer Sep 14 '13 at 12:00
  • How would you read this same string from a text file? – Vincent Ramdhanie Sep 14 '13 at 12:01

2 Answers2

0

This will split most new lines

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

However, this should be sufficient

String lines[] = theString.split("\\n");
cmd
  • 11,622
  • 7
  • 51
  • 61
  • ok thanks.as textarea will be broken by a new line character then we can split the string into string array – saitds Sep 14 '13 at 14:17
0

Sample code is for my question

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]);
saitds
  • 3
  • 1
  • 6