Results 1 to 7 of 7
  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Question about split()

    How come when I pass a String that looks like this:
    (6 + 2) + 2

    When I use the split method the first index in tokens has nothing, and then the second index has "("? The first index should have "(" not nothing. Isn't that was trim() does?
    Java Code:
    String[] tokens = infix.trim().split("");

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Question about split()

    String.split allows you to specify a delimiter to split the String by.

    Try something like this instead:
    Java Code:
    String s = "split-this-string";
    String[] sArr = s.split("-");
    String.trim() simply removes leading and trailing whitespace from the String.

    You can find out more about String methods by looking at the String java doc:
    String (Java 2 Platform SE v1.4.2)

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about split()

    If I have a string that is like this:
    Java Code:
    String str = "(6 + 2) + 2";
    How can I get an array that looks like this..?
    { "(", "6", "+", "2", ")", "+", "2" };
    Notice spaces have been taken out as well.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Question about split()

    trim() wont help you here

    Use String.replace(" ", "") to replace white space with nothing

    Then use String.toCharArray() to return a char[] array which will contain each char in a separate array element of course.
    (This is because in Java, String is really a wrapper for a char[] array)

    As I said before, read the java docs to see what you can do.

    Click the link I left above, scroll down to the Methods section. It tells you a description of each method so you'll know next time how you can do certain tasks.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about split()

    It doesn't makes sense that when I do this...
    Java Code:
    String tokens = infix.trim();
        	tokens = infix.replace(" ", "");
        	String [] token = tokens.split("");
    My array looks like this:
    { " ", "(", "6", "+", "2", ")", "+", "2" };

    Or at least it is nothing in the first index...

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Question about split()

    It's like splitting an empty string, so as the result is the original string, an empty string. That is if you have a code like:

    Java Code:
    String[] split = "".split("");
    The split array length will be 1. And it contain an empty string.

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Question about split()

    Quote Originally Posted by kraigballa View Post
    It doesn't makes sense that when I do this...
    Java Code:
    String tokens = infix.trim();
        	tokens = infix.replace(" ", "");
        	String [] token = tokens.split("");
    My array looks like this:
    { " ", "(", "6", "+", "2", ")", "+", "2" };

    Or at least it is nothing in the first index...
    You should read whats posted by ozzyman
    infix.replace(" ", "") --> your string is "(6+2)+2".
    If you would use toCharArray, you would get an array of characters { '(', '6', '+', '2', ')', '+', '2' } - is that not enough? :D
    If you really want to use split and want to have some strings like "6","+" ... try this:
    String[] token = tokens.replace(" ", "").split("(?<=.)");

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  2. split method question
    By Chasingxsuns in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 07:19 PM
  3. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •