Reputation: 373
Am Fetching few lines from remote server and saving each line as an arraylist element by setting dot as a separator. Everything works fine. I just don't understand why a strange rn
value gets appended to every new line.
E.g.:
I am going to School. I am feeling hungry
gets split and displayed as
I am going to School
rnI am feeling hungry
What does that rn
stands for?
I am using String.split
to separate. My code is below.
List<String> po= new ArrayList<String>();
separated_q = Arrays.asList(prdesc.split(Pattern.quote(".")));
for(int k=0;k<separated_q.size();k++) {
po.add(separated_q.get(k));
}
Please help me point out where am wrong.
Upvotes: 0
Views: 607
Reputation: 30985
Remember, you are only splitting on the period, so if your text is:
I am going to School.
I am feeling hungry
the program sees this (especially on Windows):
I am going to School.\r\nI am feeling hungry
The \r is the carriage return character 0x0D and the \n is the linefeed character 0x0A. The split()
isn't going to change anything with those characters, so you end up with
string[0] = "I am going to School"
string[1] = "\r\nI am feeling hungry"
You should probably be splitting on the line break itself, rather that the period. To do that, you would code this:
separated_q = Arrays.asList(prdesc.split("\\r?\\n"));
That should also get rid of the "\r\n" you are seeing.
See Split Java String by New Line
Upvotes: 1
Reputation: 359
String myString = "I love writing code. It is so much fun!";
String[] split = myString.split(".");
String sentenceOne = split[0];
String sentenceTwo = split[1];
Upvotes: 0