Results 1 to 9 of 9
- 10-18-2012, 05:21 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
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.




- 10-18-2012, 09:48 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: Split String based upon the Pattern
look in the String api, methods like indexof, substring..
- 10-18-2012, 10:03 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Split String based upon the Pattern
String.split().
The API is your friend.Please do not ask for code as refusal often offends.
- 10-18-2012, 11:07 AM #4
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
Re: Split String based upon the Pattern
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 .
- 10-18-2012, 11:18 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 10-18-2012, 08:18 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
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...
- 10-19-2012, 09:44 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 10-19-2012, 09:49 AM #8
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
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.
- 10-19-2012, 10:33 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
How to split a file by based on size
By kewlkeny in forum New To JavaReplies: 3Last Post: 01-05-2012, 11:07 AM -
How to delete files, based on a matching pattern in the filename in a directory.
By renu in forum New To JavaReplies: 6Last Post: 10-11-2011, 09:48 PM -
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks