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
2009-06-19 21:41:04
Java has the /REGEX/ notation?
Tetsujin no Oni
2009-06-19 21:43:33
No, it should be "\\n", not /\n/.
Michael Myers
2009-06-19 21:48:16
No, it doesn't. good catch - I'm used to writing Javascript.
Jeff Meatball Yang
2009-06-19 21:48:40
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.
2009-06-19 21:52:33
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
2009-06-19 21:58:30