Results 1 to 5 of 5

Thread: split problem

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default split problem

    I have a file that contains 2 lines:

    Jerry|Seinfeld|Seinfeld |Seinfeld
    Elaine|Benes|Benes|Benes {1 space after Benes}

    I am reading file into a List<String> b and then getting index 0,1 rows and splitting them by "|" to get values in each columns

    String[] a = b.get(0).split("\\|");

    I am recognizing that row 0, column 2 contains a space in "Seinfeld "
    String[] a = b.get(0).split("\\|");
    a[2] has a space

    I am failing to recognize that row 1, column 3 contains a space after "Benes "
    String[] a = b.get(1).split("\\|");
    a[3] does not have a space (BUT IT REALLY DOES)

    Please point me in the right direction

    After testing various cases, i came to a conclusion that only LAST column is affected.

    thanks

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    you have to do split("\\\\|")

    4 \\\\ because \\ is = to one \ in java. since \ is a literal character in regular expression, you need \\ in regex. if \ regex char == \\ java, \\\\ java == \\ regex

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Hang on ... but why does split works fine for all columns but last?

    String[] currentRows = makeHTMLFromOutDotTxt.get(row).split("\\|");
    System.out.println("." + currentRows[3]+".");


    this print always shows no spaces after the field, where in reality there are some

  4. #4
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Again, to be crystal clear: my file is delimited by |, any amount of white space is also included between |.

  5. #5
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    While i still would very much to hear an answer to my question, i have solved the problem

    If i am adding | at the very last column (after space), i am now able to split accurately

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. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:30 AM
  3. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 01:54 PM
  4. split Keyword
    By santhoshrao in forum New To Java
    Replies: 4
    Last Post: 08-13-2008, 10:28 AM
  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

Posting Permissions

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