How to split HTML textarea element into array of lines in Java
-
Where are you getting it from? What kind of object is it in? Can you post the code you have already?Michael Myers– Michael Myers ♦2009-06-19 21:37:10 +00:00Commented Jun 19, 2009 at 21:37
-
String notes = request.getParameter("notes"); notes is a textarea html elementSvet– Svet2009-06-19 22:08:10 +00:00Commented Jun 19, 2009 at 22:08
Add a comment
|
2 Answers
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');
3 Comments
Tetsujin no Oni
Java has the /REGEX/ notation?
Michael Myers
No, it should be "\\n", not /\n/.
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()]);
}