Java String Split Excluding Empty Values
When splitting a string using the split() method, empty values that result from the split operation are typically removed from the resulting array. This can lead to unexpected results, especially when working with strings that may contain empty values.
Consider the following code snippet:
String data = "5|6|7||8|9||"; String[] split = data.split("\|"); System.out.println(split.length);
In the above example, the expected output is an array with 8 elements: [5, 6, 7, EMPTY, 8, 9, EMPTY, EMPTY]. However, the actual output is only 6 values, excluding the empty values.
To address this issue and ensure that empty values are included in the resulting array, we can use the overloaded version of the split() method that takes an additional limit parameter set to a negative value.
String[] split = data.split("\|", -1);
By setting the limit parameter to -1, we indicate that there is no limit on the number of times the split pattern is applied. This prevents the default behavior of excluding trailing empty strings from the result array.
The split() method documentation states that if the limit is greater than zero, the pattern will be applied up to that limit and the resulting array will have a length no greater than the limit. If the limit is non-positive, the pattern will be applied as many times as possible and the resulting array can have any length.
It's important to note that the default behavior of excluding trailing empty strings only applies to empty strings that are created as a result of the split operation. If an empty string is already present in the original string, it will still be included in the resulting array.
By understanding the behavior of the split() method and utilizing the limit parameter effectively, we can control the inclusion or exclusion of empty values in the resulting array, ensuring that our splitting operations produce the desired results.
The above is the detailed content of How to Preserve Empty Strings When Splitting a Java String?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.