Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
A row such as ABC12|Hinge|4.20|| has five fields: product code, name, price, discount, and note. The last two fields are empty, not missing. Java’s one-argument String.split drops trailing empty results by default, so an importer can see only three fields and reject a valid row. For a literal pipe-delimited line, use line.split("\\|", -1) to preserve those empty columns.
Why does Java split drop empty fields at the end?
Because String.split(String regex) uses a limit of zero. Oracle’s Java SE 25 API documents that a zero limit discards trailing empty strings from the result. That behavior is easy to miss because the input still contains the delimiters; it is the returned array that loses the final empty entries.
Consider this valid five-position record:
ABC12|Hinge|4.20||
Its structure is product code, product name, price, discount, and note. The two final pipe characters mark the boundaries of two fields whose values happen to be empty. But this call uses the default zero-limit behavior:
String[] fields = line.split("\\|");
The array contains ABC12, Hinge, and 4.20; the trailing empty strings are discarded. Code that expects five fields may therefore label the record malformed, even though the supplier’s row has the expected five positions. The correction is to change the split limit, not to alter valid source data. Oracle’s Java SE 25 String API defines the overload behavior.
Popular on Amazon
- 【90 Degree Full Function USB C Adapter】Supports the latest USB4 standard for up to 40Gbps of bandwidth, 8K video support, and up to 240W Fast charging; Support USB PD 3.1 Extended Power Range (EPR) including 140W, 180W and 240W. Your productivity is easily doubled. *Warm Tip: For the best fit, please make sure the USB-C port is fully exposed; thick or bulky cases may affect compatibility.
- 【Reliable Quality】Made from high-quality, scratch-resistant aluminum alloy, delicate metal frame. get excellent wear resistance and heat dissipation. Non-Slip Design for easier insertion and removal.
- 【Small and portable】Ultra thin size, ultra compact design, a petite physique that matches the minimalist design of your modern laptops. Coming with a slim figure that won't block the port next to the one it's plugged into.
- 【Compatibility】Widely work with most usb c, thunderbolt 4/3 devices. Compatibilit for ROG Ally, Steam Deck, Switch, MacBook Pro, Quest 3 VR headset, Surface Book 2, iPhone 18/17/16/15, Samsung Galaxy S23/S22/S21, Note 20/10, Google Pixel, ChromeBook, etc.
- 【Excellence Customer Support】If you experience any issues or cable failure, simply reach out to our dedicated customer service team. We provide a prompt response within 18 hours, providing you with the necessary technical support to resolve any concerns.
It helps to count separators as boundaries rather than to count only visible values. In this row, the first pipe ends the product code, the second ends the name, the third ends the price, and the fourth ends the discount. Nothing follows the fourth pipe before the record ends, so the fifth position is an empty note. The preceding field is empty too: the third and fourth pipes are adjacent. An empty value does not erase either position.
This distinction matters in positional formats. If the importer maps array elements by index, dropping the empty discount and note can make the record look short or, in other formats, shift later values into the wrong slots. A record can be syntactically shaped correctly while containing blank values; deciding whether a blank is allowed belongs to validation, not to delimiter splitting.
How do I keep trailing empty strings with String.split?
Pass a negative limit: line.split("\\|", -1). The negative limit tells Java to apply the delimiter as much as possible and retain trailing empty results.
String line = "ABC12|Hinge|4.20||";
String[] fields = line.split("\\|", -1);
// fields has five entries:
// ["ABC12", "Hinge", "4.20", "", ""]
The fix has two separate parts:
"\\|"matches a literal pipe. The delimiter argument is a regular expression, and a bare pipe has special meaning in regex syntax. The Java string literal uses\\so the regular-expression engine receives\|, which escapes the pipe.-1retains trailing empties. It changes how the split results are limited; it is not part of the delimiter.
If the delimiter is supplied dynamically rather than written as a literal, use Pattern.quote(delimiter) to treat it as literal text rather than manually assembling regex escaping.
There are two layers of escaping to keep straight: Java first interprets the string literal, then the regex engine interprets the resulting pattern. In Java source, "\\|" produces the regex pattern \|; that pattern means “literal pipe.” This is why copying a regex fragment directly into a Java string can be confusing. For a delimiter built at runtime, quoting the delimiter with Pattern.quote avoids having to reason about its regex metacharacters individually.
Also popular on Amazon
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
What do the different limit values mean?
| Call | Effect | Use for this row? |
|---|---|---|
split("\\|") |
Uses the one-argument overload, equivalent to a zero limit; trailing empty strings are removed. | No |
split("\\|", 0) |
Makes the default zero-limit behavior explicit; trailing empty strings are removed. | No |
split("\\|", -1) |
Retains trailing empty strings. | Yes, when every delimited position matters. |
split("\\|", positiveLimit) |
Limits how many times the delimiter is applied; the final result can contain the remainder. | Only when a bounded split is intentional, not as the general fix for preserving all columns. |
For an importer that validates a fixed number of columns, a negative limit expresses the important contract directly: empty values at the end still occupy positions. A positive limit serves a different purpose, such as making the final field absorb any remaining separators. Choose it only when that is the file format’s intended behavior.
For example, a positive limit of four means Java applies the delimiter at most three times and leaves the rest of the input in the last result. That can be appropriate if the fourth field is defined to contain the unsplit remainder. It does not say “preserve every column, including empty ones,” so it is not a substitute for a negative limit when the number and positions of all fields matter.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Why does my pipe-delimited row have fewer columns?
First check the split limit and the delimiter expression. A row that ends with delimiters can produce fewer array elements than expected when the importer uses the default split behavior; an unescaped pipe can also make the regex mean something other than a literal separator.
Debug the record as a shape problem, not just as a bad-value problem. For a five-column contract, log the observed field count alongside the expected count, and retain a safe sample of rejected raw lines so the delimiter pattern and ending are visible. Handle sampled records according to your environment’s privacy and data-retention requirements.
Check the smallest reproducible case
- Use the exact raw line, including its final delimiters. A display or copy operation that trims the line can hide the cause.
- Confirm that the delimiter is literal-pipe matching, such as
"\\|"orPattern.quote("|"). - Pass
-1as the limit if every trailing empty column must remain in the result. - Compare the array length with the file’s expected field count before mapping values into an object.
- If the count is still wrong, examine whether the input follows a more complex format than simple unquoted delimiter-separated fields.
This separates two distinct failure modes: default splitting can remove trailing empty values, while a malformed delimiter expression can split the line incorrectly in the first place.
Also popular on Amazon
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Keep the original line available long enough to diagnose the discrepancy, but avoid indiscriminate logging of complete supplier records if they may contain sensitive information. A protected sample is often enough to see whether a line ends in delimiters and whether the delimiter is appearing inside a value. Record the observed and expected counts as structured diagnostics so repeated failures can be grouped and compared.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsShould I use String.split or a parser in Java?
Use corrected String.split only when the supplier’s format is genuinely simple and constrained: a known delimiter separates fields, and delimiters cannot appear inside field values. If a field can contain a pipe inside quotes, a regular-expression split is not a parser and can divide that value incorrectly.
| Concern | Corrected String.split |
Delimited-file parser |
|---|---|---|
| Trailing empty fields | Preserved with a negative limit. | Handled as part of format parsing; verify the chosen configuration. |
| Literal pipe delimiter | Escape it as a regex or quote it with Pattern.quote. |
Configure the pipe as delimiter data. |
| Quoted delimiters | Naive splitting does not safely interpret quoting. | Designed for structured quoting rules. |
| Complexity | Minimal for a fixed, simple contract. | Requires setup, but fits formats with quoting or escaping rules. |
| Operational visibility | Importer still needs rejection counts and useful diagnostics. | Parser errors and rejected records still need monitoring. |
| Maintenance | Becomes fragile as format rules grow. | Better suited to an evolving format grammar. |
Apache Commons CSV is a Java library for reading and writing variations of CSV. Its documentation and Javadocs are available at Apache Commons CSV. If you use a parser for a pipe-delimited supplier file, configure the delimiter and the relevant quoting, escaping, and line-ending rules deliberately; do not assume that a default CSV format matches the supplier’s contract.
In particular, a quoted value containing a pipe changes the parsing problem. For instance, a product name such as "Hinge | heavy duty" contains a delimiter character that should remain part of the name under a quoting convention. A simple split sees the pipe as a separator regardless of the quotes. Quoted fields, escaped quotes, and multiline values all require rules beyond “split wherever this character occurs.” Confirm the supplier’s precise rules and configure the parser to match them.
The decision is not simply short code versus long code. If the contract guarantees a fixed, unquoted delimiter format, the negative-limit split is a small, understandable solution. If the file has quoting or escaping rules, or those rules are likely to evolve, use a parser built to interpret that grammar. A parser does not remove the need for validation or monitoring: the importer still needs to handle parse errors and rejected records visibly.
Also popular on Amazon
- Portable and powerful USB-C HUB: BENFEI USB Type-C HUB, with super-soft and knot-free silicone woven design cable, meets most mobile office needs. Compact, lightweight, stylish, and powerful portable USB C Hub equipped with 1 x HDMI port, 1 x 100W charging, and 3 x USB ports. 18-month warranty, 24-hour response, to ensure you feel at ease when using our product.
- Design centered on comfort and reliability: Thanks to BENFEI's end-to-end in-house cable production capability, in-house PCBA and assembly capability, using the industry's most advanced silicone woven design and process, 20cm cable in length, no knots, super-soft, the HUB is easy to use in all scenarios: laptop, tablet, stand etc. Super-soft, 25000+ life cycles, to meet your daily carrying and office needs.
- 100W Charging: Support up to 90W USB C pass-through charging via Type-C port to keep your laptop powered. 10W is reserved for other interface operations. No data and video function on the Type-C port.
- 4K HDMI Display: The HDMI port supports media display at resolutions up to 4K 30Hz, keeping every incredible moment detailed and ultra vivid. Please note that the C port of the Host device needs to support video output.
- Transfer Files in Seconds: Transfer files and from your laptop at speeds up to 10 Gbps with USB A 3.2 port. Extra 2 USB A 2.0 ports are perfectly for your keyboards and mouse.
How can I stop valid rows from being silently rejected?
Make the importer’s assumptions observable and test the edge case that exposed them. In Serguey Asael Shinder’s Dev.to article, the importer reported “a few hundred malformed lines a night”; that is the article’s incident narrative, not an industry-wide prevalence figure. The practical lesson is to treat rejection counts as operational signals rather than discarding them as routine noise.
- Count rejected records by reason, such as wrong field count or invalid value.
- Log expected and observed field counts so a data-shape mismatch is diagnosable.
- Keep a suitably protected sample of rejected raw lines; apply the privacy and retention rules appropriate to the data.
- Alert when rejection counts change unexpectedly, rather than waiting for a downstream user to notice missing records.
- Add regression cases for a row with every value populated and a row with one or more empty trailing fields.
- If quoting is part of the contract, test a quoted field containing the delimiter before choosing or changing the parsing approach.
Separate parsing from business validation in both code and diagnostics. A record may have the expected number of positions but still fail because a required value is blank or a value has an unacceptable format. Conversely, a blank optional note is not evidence of a malformed row. Reporting these outcomes by reason helps distinguish shape errors from field-level validation failures.
Example regression cases
// All fields populated: five positions
"ABC12|Hinge|4.20|0.10|Standard"
// Final two fields empty: still five positions
"ABC12|Hinge|4.20||"
For each case, assert the expected number of fields and the values at the final positions. Testing only fully populated rows misses the precise condition that triggers the default split behavior. Keep the trailing empty-field example in the suite even if it looks unusual: it is the ordinary, valid row that exposes the assumption.
“Empty is a value.” — Serguey Asael Shinder. In a positional record, a blank field still carries structural information: it says that the position exists and its value is empty. A Java programming book or Java reference manual can be a useful optional companion for broader language and string-handling topics, but the narrow fix here is the negative split limit.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallFrequently Asked Questions
Can I use a positive limit instead of -1 if I always expect five columns?
A positive limit can cap splitting so the final result contains the remainder, which is useful when the last field is meant to absorb additional separators. It is not equivalent to preserving every trailing empty position, so use it only if that behavior matches the supplier’s format.
Also popular on Amazon
- 【Read before You Buy】 Note: This adapter is USB 2.0 version and cannot transmit HDM, VGA, or other video signals. Nevertheless, they are fully capable of charging devices,duplicating files, and can be seamlessly integrated with USB-C earphones, wireless network adapters, and SD card adapters.
- 【Easy to Use and Safe】Thanks to modern manufacturing techniques, we've made these adapters sufficiently compact so that they can remain inserted in your device without noticeable protrusion or inconvenience. This means you don't have to constantly plug and unplug them, reducing the likelihood of wear and misalignment, and making them more convenient to carry around.
- 【Reliable Quality】Engineered for enhanced durability, durable aluminum alloy shell material,get excellent wear resistance and heat dissipation,the male USB C and female USB connectors & the male USB and female USB C connectors are designed to be plugged and unplugged up to 20000 times. Provides data transfer, device charging, memory space expansion, and additional computer peripherals.
- 【Think more for you】Do you need to connect a USB-C-enabled device to an odd device that only has USB-A ports? Then these USB-A male to USB-C female adapters are made for you.Simply insert the adapter into the USB-A port on the device, and you can instantly connect your USB-C device to the device that originally didn't have the correct connection port.
- 【Trusted xiwxi Quality and Service】As a responsible brand seller, you will receive xiwxi's 12-month Quality assurance and lifetime friendly customer service, you're not just buying a product,you're investing in peace of mind with all-round quality and exceptional customer service.
What should I do if a product name can contain a pipe inside quotes?
Do not rely on String.split to interpret quotes; it only applies a regex delimiter. Use a parser configured for the supplier’s quoting and delimiter rules, and include a quoted-pipe record in the importer’s regression tests.
Does preserving empty fields prove that the whole input record is valid?
No. It preserves the record’s positional shape, but the importer still needs to validate field count, required values, and each field’s allowed format. Treat parsing and business-rule validation as separate checks.
Why does split("\\|") not keep the blank columns?
The escaped pipe identifies the delimiter as a literal pipe, but the one-argument overload still uses a zero limit. Add the second argument, -1, to retain trailing empty strings.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Should I change the supplier’s file to add placeholder values?
Not just to work around this behavior. If the supplier’s format defines empty trailing positions as valid, preserve those positions in the parser and validate the values according to the contract.
How can I tell whether the importer is rejecting rows for this reason?
Track rejection counts by reason and record expected and observed field counts. A protected sample of a rejected raw line can help confirm whether trailing delimiters are present, subject to your privacy and retention requirements.
The Bottom Line
For a simple pipe-delimited record, use line.split("\\|", -1): escape the literal pipe for regex syntax and use a negative limit to preserve trailing empty fields. If quoted values can contain delimiters, configure a real delimited-file parser instead. Empty is a value—and in a positional row, it still has a place.

