Close Menu
Şevket Ayaksız
    What's Hot

    Word Pagination: Keep a Heading with the Paragraph That Follows

    November 10, 2026

    Oracle Running Totals: Choose ROWS or RANGE Before Trusting the Result

    November 10, 2026

    Java String.split: Preserve Empty Fields When Their Position Matters

    November 10, 2026
    • software
    • Gadgets
    Şevket AyaksızŞevket Ayaksız
    • Home
    • Technology

      Apple: iPhone 17 and Older Models Hit With Unexpected $100 Price Increase

      September 10, 2026

      Apple Watch Intelligence Could Be a Major Accessibility Breakthrough

      September 10, 2026

      Apple May Skip the Base iPhone 18 This Year

      September 10, 2026

      FCC changes robot vacuum rules, potentially affecting future models

      August 8, 2026

      Samsung’s new 2TB 990 SSD drops to its lowest price yet

      August 6, 2026
    • Adobe

      Illustrator Scaling: Keep Strokes and Effects in Proportion

      November 10, 2026

      Illustrator Global Swatches: Make Color Changes Follow Your Design

      November 8, 2026

      Adobe Acrobat Bookmarks: Give Long PDFs a Useful Navigation Structure

      November 7, 2026

      How to Keep a Photoshop Adjustment on the Right Layer

      November 6, 2026

      Illustrator Linked Images: Build a Handoff That Opens Correctly on Another Computer

      November 3, 2026
    • Microsoft

      Word Pagination: Keep a Heading with the Paragraph That Follows

      November 10, 2026

      PowerPoint Selection Pane: Find and Manage Objects Behind Other Objects

      November 10, 2026

      Excel Data Validation: Guide Entry and Check What Arrives Later

      November 8, 2026

      PowerPoint Speaker Notes: Prepare the Message Behind the Slides

      November 7, 2026

      Excel Cell References: Lock Only What Your Formula Needs

      November 6, 2026
    • java

      Java String.split: Preserve Empty Fields When Their Position Matters

      November 10, 2026

      Java String Case Conversion: Choose the Locale Explicitly

      November 8, 2026

      Java Optional: Choose Between a Default Value and Deferred Work

      November 7, 2026

      Java BigDecimal Equality: Decide Whether Scale Matters

      November 7, 2026

      Java Paths: When to Normalize, Make Absolute, or Resolve the Real File

      November 6, 2026
    • Oracle

      Oracle Running Totals: Choose ROWS or RANGE Before Trusting the Result

      November 10, 2026

      Oracle NULLIF: Handle a Zero Denominator Without Inventing a Result

      November 8, 2026

      Oracle WHERE and HAVING: Filter the Right Level of Your Report

      November 8, 2026

      Oracle ROW_NUMBER: Make “Latest Record” Queries Deterministic

      November 7, 2026

      Oracle CASE Expressions: Put Overlapping Rules in the Right Order

      November 6, 2026
    Şevket Ayaksız
    Anasayfa » Java String.split: Preserve Empty Fields When Their Position Matters
    java

    Java String.split: Preserve Empty Fields When Their Position Matters

    By sevketayaksizNovember 10, 2026No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Java String.split: Preserve Empty Fields When Their Position Matters
    Share
    Facebook Twitter LinkedIn Pinterest Email

    A simple text import can fail without producing an obvious parsing error. The application receives a line, splits it into fields, and discovers fewer values than expected. The missing value may have been an empty field at the end of the line.

    Java’s String.split behavior makes this an important boundary case. Before choosing an overload, establish whether empty fields represent meaningful positions in your input format. A blank optional value and an absent column may require different handling.

    Choose the limit deliberately

    The one-argument split method uses behavior equivalent to a limit of zero, which discards trailing empty strings. A negative limit preserves them. A positive limit restricts the number of splits and leaves the remaining input in the last array element.

    Consider this deliberately simple, unquoted record:

    String record = "A17,active,";
    
    String[] compact = record.split(",");
    String[] complete = record.split(",", -1);

    The first result contains two fields. The second contains three, with an empty string in the final position.

    Suppose the agreed layout is identifier, status, and optional comment. The third position still exists when the comment is blank. Preserving it allows the importer to distinguish that valid record from a line that never supplied the third field.

    Remember that the separator is a regular expression

    The first argument to split is a regular expression. Characters with special regex meanings need appropriate treatment when the intended separator is literal.

    For a configurable delimiter, Pattern.quote produces a pattern that matches the supplied text literally. The Pattern API documents this behavior explicitly.

    For example:

    import java.util.regex.Pattern;
    
    String delimiter = "|";
    String record = "A17|active|";
    String[] fields = record.split(Pattern.quote(delimiter), -1);

    This example expresses two independent decisions: the vertical bar is a literal delimiter, and trailing empty fields must remain in the result.

    Keeping those decisions visible makes the code easier to review. A future change from a comma to a different separator should not silently change how the parser interprets that separator.

    Validate the structure after splitting

    Preserving empty values does not prove that a record is valid. Check the expected field count before reading positions, then validate each field according to its role.

    For the three-field example, reject an unexpected fourth field rather than quietly ignoring it. Check that the identifier is present, that the status belongs to the accepted set, and that a blank comment is allowed.

    Useful test cases include a normal record, an empty final field, an empty middle field, too many fields, and too few fields. Give each case an expected outcome. This makes the format’s rules explicit instead of letting incidental library behavior define them.

    Keep CSV parsing separate

    Real CSV can contain quoted commas, embedded line breaks, and escaped quotation marks. RFC 4180 describes these conventions. Splitting each line at every comma cannot represent those cases correctly.

    Use String.split for a format whose rules are genuinely simple and controlled. For CSV exchange, choose a parser that understands the required dialect.

    The engineering decision begins with the input contract. Once that contract says what empty values, quoting, and delimiters mean, the implementation can preserve those meanings reliably.

    Sources

    • Oracle — Java String API
    • Oracle — Java Pattern API
    • RFC Editor — RFC 4180: CSV format
    Views: 1
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    sevketayaksiz
    • Website

    Related Posts

    Java String Case Conversion: Choose the Locale Explicitly

    November 8, 2026

    Java Optional: Choose Between a Default Value and Deferred Work

    November 7, 2026

    Java BigDecimal Equality: Decide Whether Scale Matters

    November 7, 2026
    Add A Comment

    Comments are closed.

    Şevket Ayaksız
    Instagram
    • Home
    • Adobe
    • microsoft
    • java
    • Oracle
    • Contact
    © 2026 Theme Designed by Şevket Ayaksız.

    Type above and press Enter to search. Press Esc to cancel.