File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Alternative to String.split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative to String.split()" Watch "Alternative to String.split()" New topic
Author

Alternative to String.split()

Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

Hi Folks,
In my code, I am attempting to split a String.
The String I have to split is huge and so this method returns very very big array causing heap space problems.
I have considered first splitting the String into smaller managable Strings and then splitting them but it looks a little messy to me.

Could you please suggest a more efficient and cleaner way to do this.

Thanks in advance,
Royston


Cheers,
Roy.
Sun Certified Java Programmer 1.5,
Sun Certified Web Component Developer 5.
"No pain. No Gain"
http://cs.nyu.edu/~rpm319
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

You could use StringTokenizer, but that can't handle regular expressions to split on, only single characters. The alternative is to use Pattern and Matcher; in fact these are also used by String.split (which in fact uses Pattern.split). Roughly speaking:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is the string regular? In other words, can it be parsed? Is it line-oriented (or other delimiter)? Why is it so big?
Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

Thanks a lot Rob. The parser and Matcher worked like a breeze perfectly.



The String is a set of input messages that my code gets to work on. I am given this large string to parse and convert in to another format. It is a regular String that has to be parsed/split with \n \r as the delimiter.


David Newton
Post Friday, September 04, 2009 7:20:51 PM Subject: Alternative to String.split()
Is the string regular? In other words, can it be parsed? Is it line-oriented (or other delimiter)? Why is it so big?


Apologies for the delay. Was on holiday during the weekend
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

If \n and \r are the delimiters, perhaps a BufferedReader wrapped around a StringReader is a better idea.
Javed Inamdar
Greenhorn

Joined: Mar 30, 2008
Posts: 13
You can use reference/related examples from www.java2examples.com for more details
Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

Thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Alternative to String.split()