0

I am using a text area in a jsp page. In a servlet, I am trying to fetch multiple lines from the text area. I used

textarea.getParameter("textarea");

and

textarea.getParameterValues("textarea");

both return 2 sentences in the text area with a newline in between them. My requirement is that I should use the 2 sentences in the text area separately. How do I separate the 2 sentences in servlet?

Edit:

The input is Input

I need to get these two sentences in a servlet and split them and use them individually.

Joker
  • 81
  • 12
  • Is it always two lines? Or is it just that you want to split `n` lines of input into separate lines? In the first case I would use two inputs instead of one. In the second, check out [String#split](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) – NilsH Feb 10 '15 at 08:00
  • possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – PhoneixS Feb 10 '15 at 08:06
  • @NilsH not just 2 lines, its n number of lines – Joker Feb 10 '15 at 09:12

1 Answers1

2

Use java.lang.String.split(String regex):

String s = "First sentence.\nSecond sentence.";
String[] sentences = s.split("\n");
System.out.println(sentences[0]);
System.out.println(sentences[1]);

Output:

First sentence.
Second sentence.
  • I tried using split("\n") but sentences[0] prints with newline and sentences[1] prints the same line (ie) sentences[0]: I am Rahul (A empty line in between) and then sentences[1]: I study MBA – Joker Feb 10 '15 at 09:10
  • Please inlcude the *exact* input you try to split. Please [edit](https://stackoverflow.com/posts/28426653/edit) your question with it –  Feb 10 '15 at 09:17