Results 1 to 6 of 6
  1. #1
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default Using String Split

    I have a pipe-delimited string in a variable named system.
    I want to split it, dividing on the pipe character:

    String[] x = system.split("|");


    The above statement does not work like I wanted.
    What is the argument I need to pass to the split method?

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What's the string you want to split? What's the ouput? What do you expect? What does the API say about | http://java.sun.com/j2se/1.5.0/docs/...x/Pattern.html
    Last edited by PhHein; 07-27-2010 at 07:38 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JonJacobs View Post
    I have a pipe-delimited string in a variable named system.
    I want to split it, dividing on the pipe character:

    String[] x = system.split("|");


    The above statement does not work like I wanted.
    What is the argument I need to pass to the split method?

    Thanks,
    The split( ... ) method takes a String representation of a regular expression (RE). A RE uses the pipe symbol | as a 'meta symbol', i.e. it has a special meaning for REs. You have to escape it with a backslash \| but unfortunately the backslash character is also special to javac, the Java compiler so you have to escape it twice: "\\|". Feed that String to your split( ... ) method and see for yourself.

    kind regards,

    Jos

  4. #4
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    >What's the string you want to split?
    >>I have a pipe-delimited string in a variable named system.
    "this|that|other"

    >What's the ouput?
    numbers

    >What do you expect?
    this
    that
    other
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Fine :) Now do what Jos has posted in detail.
    WTF?! Numbers?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    >you have to escape it twice: "\\|".
    Thank you very much. That worked!
    Jon Jacobs
    This message was composed entirely from recycled electrons.

Similar Threads

  1. Using an asterisk to split a string.
    By Cruncher in forum New To Java
    Replies: 2
    Last Post: 06-13-2010, 03:12 PM
  2. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  3. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  4. 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
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Tags for this Thread

Posting Permissions

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