Question

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();
                    } 
Était-ce utile?

La solution 2

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]);

Autres conseils

This will split most new lines

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

However, this should be sufficient

String lines[] = theString.split("\\n");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top