Split Java String by New Line

Split Java String by New Line

This should cover you:

String lines[] = string.split("\\r?\\n");

There's only really two newlines (UNIX and Windows) that you need to worry about.

How to split string by comma and newline (\n) in Java?

public class Test {
public static void main(String[] args) {
String string = "New York,USA,1000\n" + "City,World,2000\n";
String[] newline = string.split("\n");
String[] result = new String[1000];
//first loop
result = newline[0].split(",");
System.out.println(Arrays.toString(result));
//second loop
result = newline[1].split(",");
System.out.println(Arrays.toString(result));
}
}
/*
output:
[New York, USA, 1000]
[City, World, 2000]
*/

result would be overrided instead of append.

public class Test {
public static void main(String[] args) {
String string = "New York,USA,1000\n" + "City,World,2000\n";
String[] newline = string.split("\n");
String[] result = new String[1000];
int index = 0;
for (String s : newline) {
for (String t : s.split(",")) {
result[index++] = t;
}
}
for (int i = 0; i < index; i++) {
System.out.print(result[i] + " ");
}
}
}
/*
output:
New York USA 1000 City World 2000
*/

How to split a java string based on newline character

Thanks everybody for replying. But i got it working using following approach:

String pattern = Pattern.quote("\\" + "n");
String lines[] = toUnicodeEncoded.split(pattern);

Separate string by whitespace, but keep newlines in split array

You could first replace all \n with \n (newline and a space) and then do a simple split on the space character.

    String input = "Hello \n\n\nworld!";
String replacement = input.replace("\n", "\n ");
String[] result = replacement.split(" ");
  • input: "Hello \n\n\nworld!"
  • replacement: "Hello \n \n \n world!"
  • result: ["Hello", "\n", "\n", "\n", "world!"]

Note: my example does not handle the final exclamation mark - but it seems you already know how to handle that.

Split string by new line symbol

I'm not sure if I understand you correctly but I see that your file contains \n literals (not actual line breaks). If you want to split on them then you need to write split as

split("\\\\n")

Regex which represents \ literal looks like \\ (it needs to be escaped, otherwise single \ will be treated for example as start of predefined character class like \d which represents digits).

But String which will represents such regex \\ need to be written as "\\\\" because in string \ is also special and it also needs to be escaped.

In short to match \ with regex you need to escape it in two levels:

  • in regex \\
  • in String "\\\\".

splitting string by spaces and new lines

I hope i understood your question right but if you're looking to extract the number and return them in the specific format you could go like this :

    // Assuming the String would be like a repetition of [word][space][number][b][\n]
String testString = "xyz 100b\nabc 200b\ndef 400b";

// Split by both endOfLine and space
String[] pieces = testString.split("\n| ");

String answer = "";

// pair index should contain the word, impair is the integer and 'b' letter
for (int i = 0; i < pieces.length; i++) {
if(i % 2 != 0 ) {
answer = answer + "answer " + ((i/2)+1) + ": " + pieces[i] + "\n";
}
}
System.out.println(answer);

This is the value of answer after execution :

answer 1: 100b
answer 2: 200b
answer 3: 400b


Related Topics



Leave a reply



Submit