Results 1 to 8 of 8
  1. #1
    abhiN is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default problem with split method

    Hi

    i m using split methos and want to display string elements seperatly..

    String quoteValues = ";;12";
    String[] quote = quoteValues.split(";");
    for(int i=1 ; i<quote.length ; i++)
    {
    if(quote[i] != "")
    System.out.println(" quote--"+quote[i]);
    }

    but i m getting output--
    quote--
    quote--12

    i want only to get--
    quote--12

    how to get..
    plz reply..

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    if(!quote[i].equals(""))
    Pointer: ==, is String a native type ??
    Last edited by roots; 01-17-2008 at 05:41 AM.
    dont worry newbie, we got you covered.

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    The split method recognizes your token, in this case ';' and prints out the strings before or after it, if they exist - but not including the token. So after reading the first token, it looks to see surrounding strings, and finds ";12" - thus it finds another token which it can't print resulting in the undesired output. From there, I assume you know how split() operates. The pattern you're looking for is to exclude empty strings, thus:

    Java Code:
    String[] quote = quoteValues.split(";+");
    Hope this helps. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    abhiN is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default

    thanks..it works.. my simple mistake..

  5. #5
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Facing problem with some charecters

    I have used the split API with "|" and i am not getting the proper result.
    Can somebody tell me why..
    the sample code is given under

    public static void main(String[] args) {


    String a = "a|c|d|e";
    String b[] = a.split("|");

    for(int i=0; i<b.length; i++){
    System.out.print(b[i]+" ");
    }
    System.out.println(b.length);


    }

    and the result i got is:
    a | c | d | e 8

    any idea abt this?

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Meta character

    Your problem is that "|" is a regex metacharacter:
    The metacharacters supported by this API are: ([{\^-$|]})?*+.
    Try replacing "|" with "\\|"
    Here's a good guide for regex:

    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    Luck,
    CJSL

    PS: Please open your own post... don't post on other old posts.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Wink it worked

    Thanks.... It worked after using the "\\|" in split

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Please mark as Solved with Thread Tools -> Mark as Solved at the top of the page (unless you have more questions).
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. 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
  2. how to split a file
    By nagaraaju in forum New To Java
    Replies: 0
    Last Post: 03-14-2008, 08:45 AM
  3. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM
  4. Problem with sort method
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 07:12 PM
  5. PDF Split and Merge 0.7 beta 1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-24-2007, 08:46 AM

Posting Permissions

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