• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Alternative to String.split()

 
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If \n and \r are the delimiters, perhaps a BufferedReader wrapped around a StringReader is a better idea.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use reference/related examples from www.java2examples.com for more details
 
Royston Monteiro
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic