views:

2052

answers:

2

How to split HTML textarea element into array of lines in Java

+1  A: 

If you mean Java and not Javascript (assuming you have a JSP system):

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

or

String[] lines = request.getParameter("textarea").split("\\n");


Edited down:

For javascript:

var lines = document.getElementById("myTextArea").value.split('\\n');
Jeff Meatball Yang
Java has the /REGEX/ notation?
Tetsujin no Oni
No, it should be "\\n", not /\n/.
Michael Myers
No, it doesn't. good catch - I'm used to writing Javascript.
Jeff Meatball Yang
I'm not using JTextArea so getText() wont work. I'm getting the textarea content with request.getParameter("textarea") which returns string. No it is not JS.
A: 

In Java, get the text and pass it to a method like:

private String[] split(String s) {
        if (s==null) {
            return new String[0];
        }

        StringTokenizer st = new StringTokenizer(s,"\n");
        ArrayList list = new ArrayList();
        while (st.hasMoreElements()) {
            list.add(st.nextToken());
        }

        return (String[]) list.toArray(new String[list.size()]);
}
Bruno Rothgiesser