Results 1 to 8 of 8
  1. #1
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Split a String with split()--Help

    Hi everyone,

    I am taking an array from a webpage and I am putting all its html code in a string (k). Now I want to split that string so that I will only have the info in a table (each line in a seperate cell). I want to split it (using </tr> as delimiter) but keep the </tr> and not loose it. How can I do that?

    String[] x = k.split("</tr>"); //This line looses the </tr>.
    I would also appreciate any other suggestions.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by danilson View Post
    Hi everyone,

    I am taking an array from a webpage and I am putting all its html code in a string (k). Now I want to split that string so that I will only have the info in a table (each line in a seperate cell). I want to split it (using </tr> as delimiter) but keep the </tr> and not loose it. How can I do that?

    String[] x = k.split("</tr>"); //This line looses the </tr>.
    I would also appreciate any other suggestions.
    Glue it back on afterwards?

    Java Code:
    for (int i= 0; i < x.length; i++)
       x[i]+= "</tr>";
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Well...thanks. Of course this works. But I think there is a way of using split without loosing the delimiter and I can't find and use it properly. It is something like
    x.split("(?<=[!])") if the ! is the delimiter, but I can't adjust it into my case.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by danilson View Post
    Well...thanks. Of course this works. But I think there is a way of using split without loosing the delimiter and I can't find and use it properly. It is something like
    x.split("(?<=[!])") if the ! is the delimiter, but I can't adjust it into my case.
    Reluctant quantifiers don't do anything for the split( ... ) method; what you have in mind won't work, i.e. that method removes the delimiter String/regular expressions.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

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

    Default

    Quote Originally Posted by JosAH View Post
    Reluctant quantifiers don't do anything for the split( ... ) method
    Mhm? That is not a reluctant quantifier or? Thats a special construct and is standing for:
    "(?<=X) X, via zero-width positive lookbehind"

    Pattern (Java Platform SE 6)

    as an example
    Java Code:
    		String k = "<tr>hello</tr><tr>world</tr>";
    		String[] x = k.split("(?<=</tr>)");
    		System.out.println(Arrays.toString(x));
    the output is : [<tr>hello</tr>, <tr>world</tr>] but if </tr> is not the end of the string, the balance after </tr> will find a place in your array too :)

    can you give an example, how your string looks like and and how you want your array to be filled?

  6. #6
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    Mhm? That is not a reluctant quantifier or? Thats a special construct and is standing for:
    "(?<=X) X, via zero-width positive lookbehind"

    Pattern (Java Platform SE 6)

    as an example
    Java Code:
    		String k = "<tr>hello</tr><tr>world</tr>";
    		String[] x = k.split("(?<=</tr>)");
    		System.out.println(Arrays.toString(x));
    the output is : [<tr>hello</tr>, <tr>world</tr>] but if </tr> is not the end of the string, the balance after </tr> will find a place in your array too :)

    can you give an example, how your string looks like and and how you want your array to be filled?
    This was what I was looking for. I did something very stupid and it didn't work before. I used "" in a wrong way. Thanks a lot.

  7. #7
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Oh, and by the way, </tr> is the end of my string so there is no problem. Thanks again

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by eRaaaa View Post
    Mhm? That is not a reluctant quantifier or? Thats a special construct and is standing for:
    "(?<=X) X, via zero-width positive lookbehind"
    Friday afternoon and I've learned something new; thanks for that.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. String split
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 07:51 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

Posting Permissions

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