Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to String.split()

 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If \n and \r are the delimiters, perhaps a BufferedReader wrapped around a StringReader is a better idea.
 
Javed Inamdar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use reference/related examples from www.java2examples.com for more details
 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic