Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: String split(): I want it to include the empty strings at the end [duplicate]

Tags:

java

string

split

I have the following String:

String str = "\nHERE\n\nTHERE\n\nEVERYWHERE\n\n"; 

If you just print this, it would output like this (Of course the \n wouldn't be "literally" printed):

\n HERE\n \n THERE\n \n EVERYWHERE\n \n \n 

When I call the method split("\n"), I want to get all strings between the new line (\n) characters even empty strings at the end.

For example, if I do this today:

String strArray[] = str.split("\n");  System.out.println("strArray.length - " + strArray.length); for(int i = 0; i < strArray.length; i++)     System.out.println("strArray[" + i + "] - \"" + strArray[i] + "\""); 

I want it to print out like this (Output A):

strArray.length - 8 strArray[0] - "" strArray[1] - "HERE" strArray[2] - "" strArray[3] - "THERE" strArray[4] - "" strArray[5] - "EVERYWHERE" strArray[6] - "" strArray[7] - "" 

Currently, it prints like this (Output B), and any ending empty strings are skipped:

strArray.length - 6 strArray[0] - "" strArray[1] - "HERE" strArray[2] - "" strArray[3] - "THERE" strArray[4] - "" strArray[5] - "EVERYWHERE" 

How do I make the split() method include the empty strings like in Output A? I, of course, could write a multi-line piece of code, but wanted to know, before I waste my time trying to implement that, if there was a simple method or an extra two or so lines of code to help me. Thanks!

like image 625
Rob Avery IV Avatar asked Dec 18 '12 19:12

Rob Avery IV


People also ask

Does split () alter the original string?

Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.

Can you split an empty string?

The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

What happens when split empty string?

The natural consequence is that if the string does not contain the delimiter, a singleton array containing just the input string is returned, Second, remove all the rightmost empty strings. This is the reason ",,,". split(",") returns empty array.

How to split a string in Java?

The string split () method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a char array. Following are the two variants of the split () method in Java: Attention reader! Don’t stop learning now.

How do I split an empty string into an array?

From the documentation of String.split (String regex): This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

What is the return type of the split method in Java?

The method returns an array of strings. The declaration contains the following words: "In Java, the split method splits a string into substrings." The method collects these substrings into an array that becomes the return value. The method has a string input parameter called regex.

What is the limit of the string split method?

This variant of the split method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here, by default limit is 0. Returns: An array of strings is computed by splitting the given string.


1 Answers

use str.split("\n", -1) (with a negative limit argument). When split is given zero or no limit argument it discards trailing empty fields, and when it's given a positive limit argument it limits the number of fields to that number, but a negative limit means to allow any number of fields and not discard trailing empty fields. This is documented here and the behavior is taken from Perl.

like image 95
hobbs Avatar answered Sep 25 '22 05:09

hobbs