2

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

2
  • Where are you getting it from? What kind of object is it in? Can you post the code you have already? Commented Jun 19, 2009 at 21:37
  • String notes = request.getParameter("notes"); notes is a textarea html element Commented Jun 19, 2009 at 22:08

2 Answers 2

4

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

For JavaScript:

var lines = document.getElementById("myTextArea").value.split('\\n');
Sign up to request clarification or add additional context in comments.

3 Comments

Java has the /REGEX/ notation?
No, it should be "\\n", not /\n/.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.