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 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

Related Questions

I'm noticing a strange behaviour in Firefox. I have a banner that's positioned absolutely
I'm noticing strange behavior. I have merchant and order tables and doing two selects
I have been noticing some very strange usage of O(1) in discussion of algorithms
I am using SQL Server 2005 and I have noticed something strange when I
I am noticing a strange issue with respect to spring XML schemas. I have
I started noticing strange behavior when navigating the main toolbar of my Winforms application,
I'm noticing some strange results with date conversions between MYSQL and PHP. Please see
I'm noticing short URL sites are using alphnumeric id instead of integers for unqiue
I'm noticing some strange behavior when I add a ComboBox to my Windows Form
I'm noticing some conversations not getting closed, staying in CONVERSING state. The strange thing

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.