Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: String.split("") puts null in the first index

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default String.split("") puts null in the first index

    So I have absolutely no idea why this is happening, hopefully someone can help me out. I'm a novice programmer, and lately I've noticed that whenever i use String.split() with "" as a delimiter, the first index will be null and the second index will contain the first element, and so on. Here's an example of some output i've been seeing:

    public class wtf
    {
    	public static void main(String[] args)
    	{
    		String mystring = "hello";
    		String[] myarray = mystring.split("");
    		for(int i=0; i<myarray.length;i++)
    			System.out.println(i + ": "+ myarray[i]);
    	}
    }


    OUTPUT:
    0:
    1: h
    2: e
    3: l
    4: l
    5: o


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String.split("") puts null in the first index

    The first element in the array looks like an empty String, not null.
    I don't understand regular expressions to be able to tell you what an empty regexp String will do.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: String.split("") puts null in the first index

    use trim before apply split function. let me know if it doe'st not solve your problem

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String.split("") puts null in the first index

    I don't see a blank in the String that the OP posted. Do you? What would trim do with no spaces?

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String.split("") puts null in the first index

    this is probably too late to get another response, but kB18001 I tried String.trim() first but to no avail, I get the same output

  6. #6
    Junior Member
    Join Date
    Aug 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String.split("") puts null in the first index

    did you solve the problem?

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Casting "null" when and why ?
    By Learner in forum Java Theory & Questions
    Replies: 2
    Last Post: August 16th, 2011, 06:41 PM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM