Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8020672

String.split() return non-empty array on empty string

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 7u25
    • core-libs

      FULL PRODUCT VERSION :
      java version " 1.7.0_25 "
      Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When used on an empty character sequence the method String.split(String, int) returns an array containing the original String.

      According to the documentation of this method one would expect an empty array:
       " 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. "

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      System.out.println( " " .split( " , " , 0).length);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output should be " 0 "
      ACTUAL -
      Output is " 1 "

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.Example;

      import static org.junit.Assert.*;

      import org.junit.Test;

      public class StringTest {

          @Test
          public void testSplitOnEmptyString() {
              assertEquals(0, " " .split( " , " , 0).length);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Check for empty character sequence before calling split()

            mchung Mandy Chung (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: