Results 1 to 9 of 9
  1. #1
    pavan528 is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Red face Split String based upon the Pattern

    Hi all,
    I have a Text file with following Entries.
    (arule,12),(brule,21),(zrule,70),(drule,25),(erule ,10)
    (frule,3)
    (grule,20),(srule,100)

    I need to read a line at a time and split them as separate ordered pairs into another Array( i mean a[0]=(arule,12) and a[1]=(brule,21) and so on). I am not sure how this could be done in java as I am very new to it. please share any of the idea of how to do it.


  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Split String based upon the Pattern

    look in the String api, methods like indexof, substring..

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Split String based upon the Pattern

    String.split().
    The API is your friend.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Split String based upon the Pattern

    Quote Originally Posted by Tolls View Post
    String.split().
    The API is your friend.
    In the same way that a car owner's manual shows one how to drive a car.

    For this problem the OP will need to understand the concepts of look-behind and look-ahead and will almost certainly need to go well beyond the Javadoc. He will need to consult a tutorial such as Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes) and/or Regular Expression Tutorial - Learn How to Use Regular Expressions .

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Split String based upon the Pattern

    For some reason I thought there was a link to the tutorial in the Pattern API (referenced from the split() Javadoc).
    But apparently there isn't.

    Still, research is a good skill to learn...that's my excuse, and I'm sticking to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    pavan528 is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: Split String based upon the Pattern

    Hi Tolls,
    I can use String.Split method to count the number of ',' in the line and enter the element in the array at each even occurence of the character. But I feel there is a better approach to this using regular expression but I am not sure how to build the regular expression for this combined characters ")," so that I can ebnter the values upto that occurence. if any idea of building the regex for this combination of characters. please let me know the direction to that. Thanks a lot for your time.... and replies...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Split String based upon the Pattern

    Well yes, split uses a regex, which is why sabre150 pointed you to a couple of regex tutorials.
    I'm not a regex person by any means, but I would recommend going through those tutorials and trying to piece one together.
    You can Google for regex testers, which will allow you to experiment against a sample of one of your lines.

    Indeed if you are using Eclipse there is at least one plugin to do the same thing, the one I have is QuickREx.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: Split String based upon the Pattern

    Use split("pattern") in String class.

    See [Blog spam removed]
    Last edited by DarrylBurke; 12-31-2012 at 04:10 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Split String based upon the Pattern

    That has already been pointed out in this thread.
    Is there really any need to add to what's been said?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to split a file by based on size
    By kewlkeny in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 11:07 AM
  2. Replies: 6
    Last Post: 10-11-2011, 09:48 PM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 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
  •