Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:08:13+00:00 2026-06-07T19:08:13+00:00

When there are adjacent separators in the split expression I expect null or an

  • 0

When there are adjacent separators in the split expression I expect null or an empty string–not have it eliminated.

The Java code is below:

public class splitter {
    public static void main(String args[]) {
        int size = "||".split("\\|").length;
        assert size == 3 : "size should be 3 and not " + size;
    }
}

I expected to get either { “”, “”, “” } or { null, null, null }. Either would be fine.

Perhaps there’s a regular expression that will not be fooled by empty words?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:08:15+00:00Added an answer on June 7, 2026 at 7:08 pm

    According to the javadoc:

    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.

    The javadoc for split(String, int) elaborates:

    The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n – 1 times, the array’s length will be no greater than n, and the array’s last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

    (emphasis mine)

    So to return an array of empty strings, call "||".split("\\|", -1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems swapping adjacent nodes in a linkedlist. for ex: input : 1->2->3->4->5->null
When I split a string in python, adjacent space delimiters are merged: >>> str
We all know how the adjacent selector works (p+p), but I wonder if there's
I'm writing regular expression for checking if there is a substring, that contains at
I have an elevation map represented by a 2D array of floats. There are
Is there any way to have a CSS class to parse text input as
On Ring Buffer's Wikipedia entry , there's example code showing a hack for UNIX
I have the following problem: There are N items A(n) and associated weights W(n)
I needed a String tokenizer in Haskell but there is apparently nothing already defined
I have been trying to solve a PHP regular expression problem for awhile now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.