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

  • SEARCH
  • Home
  • 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 908123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:40:00+00:00 2026-05-15T16:40:00+00:00

Case 1 String a = ; String[] b = a.split(,); System.out.println(b.length); Prints 1. Why?

  • 0

Case 1

String a = "         ";
String[] b = a.split(",");
System.out.println(b.length);

Prints 1. Why?

Case 2

String a = ",,,,,,,,,,,,";
String[] b = a.split(",");
System.out.println(b.length);

Prints 0. Why?

Honestly, i am at a loss here

  • 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-05-15T16:40:01+00:00Added an answer on May 15, 2026 at 4:40 pm

    This behaviour is mentioned in the documentation for String.split:

    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.

    Your first example should give an array containing a single string containing spaces. A string containing spaces is not empty so it is included in the result.

    Your second example would give an array containing lots of empty strings, but these are not included in the resulting array as mentioned in the documentation.

    As to why the Java designers decided that removing trailing empty strings when limit is zero is a good idea – I don’t know. Most other programming languages / platforms do not do this. I consider it to be a “gotcha” – a feature that doesn’t quite work as most people expect.

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

Sidebar

Related Questions

What I'm looking to do is split data from string into an array. Here's
How can I convert a mixed case string to a lowercase string in C?
Possible Duplicate: How do I do a case insensitive string comparison in Python? I
I'm trying to add a value (in this case the string I Added This)
What's the easiest way to do a case-insensitive string replacement in Python?
I've read that it's unwise to use ToUpper and ToLower to perform case-insensitive string
When trying to use a Switch Case with switch(string) for an android project ,
Consider this case: public Class1 { public static final String ONE = ABC; public
How do I take a string and convert it to lower or upper case
I have a string: hello good old world and i want to upper case

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.