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 538579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:02:48+00:00 2026-05-13T10:02:48+00:00

I am noticing strange behaviour when using the split() method in Java. I have

  • 0

I am noticing strange behaviour when using the split() method in Java.

I have a string as follows: 0|1|2|3|4|5|6|7|8|9|10

String currentString[] = br.readLine().split("\\|");
System.out.println("Length:"+currentString.length);
for(int i=0;i < currentString.length;i++){
     System.out.println(currentString[i]);
}

This will produce the desired results:

Length: 11
0
1
2
3
4
5
6
7
8
9
10

However if I receive the string: 0|1|2|3|4|5|6|7|8||

I get the following results:

Length: 8
0
1
2
3
4
5
6
7
8

The final 2 empties are omitted. I need the empties to be kept. Not sure what i am doing wrong. I have also tried using the split in this manner as well. …split("\\|",-1);

but that returns the entire string with a length of 1.

Any help would be greatly appreciated!

  • 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-13T10:02:48+00:00Added an answer on May 13, 2026 at 10:02 am

    The default behavior of split is to not return empty tokens (because of a zero limit). Use the two parameter split method with a limit of -1 will give you all empty tokens in the return.

    UPDATE:

    Test code as follows:

    public class Test {
        public static void main(String[] args) {
        String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|", -1);
        System.out.println("Length:"+currentString.length); 
        for(int i=0;i < currentString.length;i++){ System.out.println(currentString[i]); }
      }
    }
    

    Output as follows:

    Length:11
    0
    1
    2
    3
    4
    5
    6
    7
    8
    --- BLANK LINE --    
    --- BLANK LINE --
    

    The “— BLANK LINE –” is put in by me to show that the return is blank. It is blank once for the empty token after 8| and once for the empty trailing token after the last |.

    Hope this clears things up.

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

Sidebar

Ask A Question

Stats

  • Questions 305k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer myClass needs a custom assignment operator, in addition to the… May 13, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer Don't think you can save the buffer without writing some… May 13, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer The closest you might get to a formal definition of… May 13, 2026 at 9:10 pm

Related Questions

I have this web app that is running ASP .NET MVC 1.0 with LINQ
I'm not sure if it's my system, although I haven't done anything unusual with
Now that my OpenGL application is getting larger and more complex, I am noticing
I am dynamically generating a table to present some tabular data. Normally I would
I am using SetWindowPos() to try and send Windows to back/front of the z-order.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.