Tuesday, July 23, 2013

Split String into Fixed Length chunks using Java

I needed to split a large string into fixed length chunks of equal size using Java Apart from the regular method of looping and doing a substring for the required length, I was wondering in what other way was it possible to achieve the same. Of course, it's Mr. Regex to the rescue for this task at hand! Here was the regex I used to do this:
String largeString = "This is a very large and totally useless and meaningless string";
int chunkLength = 5;
String[] chunks = largeString.split("(?<=\\G.{" + chunkLength + "})");
System.out.println("Number of chunks: " + chunks.length); // Should print 13
System.out.println("Chunk size: " + chunks[0].size()); // Should print 5 == chunkLength
The regular expression is a Positive Look-Behind looking for any chunkLength characters beginning at the position where the last match ended. So the first time around, it matches the beginning of the string and then after that, it keeps matching every set of chunkLength characters.
This makes me think I should write a more detailed post about regular expressions, especially on Look-Ahead and Look-Behind - stay tuned!

25 comments:

brunagari said...

Really nice post.Thanks for sharing.
jobbörse siegerland

brunagari said...


Your website content nice nice and interesting to observe.
jobbörse Neunkirchen

SachinVarshan said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
python training in Chennai

brunagari said...

really good
Jobvermittlung

brunagari said...


amazing
personalrekrutierung,

Unknown said...

Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Pega Training in Chennai

Unknown said...

This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.... Java Training in Chennai | Blue prism Training in Chennai

Unknown said...

The strategy you have posted on this technology helped me to get into the next level and had lot of information in it... Java Training in Chennai | Blue prism Training in Chennai

srihariparu said...

Really Very happy to see this blog. thanks for sharing such a amazing blog...
Best IOT Project Center in Chennai | IOT Project Center in Velachery | IOT Projects in Guindy

srihariparu said...

Very interesting blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog..
Java Project Center in Chennai | Java Project Center in Velachery | Java Projecs in Perungudi

srihariparu said...

Very interesting blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog..
Java Project Center in Chennai | Java Project Center in Velachery | Java Projecs in Perungudi

srihariparu said...

Very interesting blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog..
Java Project Center in Chennai | Java Project Center in Velachery | Java Projecs in Perungudi

deepti said...

Thanks to share the useful information on your blog,
Cloud Computing Project Center in Chennai | Cloud Computing Project Center in Velachery | Cloud Computing Projects in Tambaram

srihariparu said...

Nice and interesting article to read..... keep updating
MBA Project Center in Chennai | MBA Project Center in Velachery | MBA Projects in Velachery | MBA Projects in Taramani

deepti said...

Awesome Blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog.
Java Project Center in Chennai | Java Projects Center in Velachery | Java Projects in Perungudi

srihariparu said...

Really Very happy to see this blog. thanks for sharing such a amazing blog...
Image Processing Project Center in Chennai | Image Processing Projects in Velachery | Image Processing Projects in Taramani

srihariparu said...

Very interesting, good job and thanks for sharing such blog. It is very interesting to read. Its pretty good and well noted.
Cloud Computing Project Center in Chennai | Cloud Computing Projects in Velachery

deepti said...

Thanks for giving nice information and informative blog...It's really an amazing post..keep updating..
MBA Project Center in Chennai | MBA Projects in Velachery | MBA Projects in Perungudi | MBA Finance Projects in Velachery | MBA HR Projects in Velachery | MBA Marketing Projects in Velachery

srihariparu said...

Excellent Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge...
Final Year Projects for BCA in Chennai | Final Year Projects for BCA in Velachery | Final Year Projects for MCA in Velachery | Final year Projects in Velachery

deepti said...

Very nice information to updating..Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
Electrical Project Center in Chennai | Electrical projects in Velachery | Electrical Projects in St.thomas Mount

srihariparu said...

Thanks for sharing this information, it helped me a lot in finding valuable resources for my career
Java Training Institute in Chennai | Java Training Center in Velachery | java Training in Chennai

srihariparu said...

I have read your blog. Your information is really useful for beginner. information provided here are unique and easy to understand.Thanks for this useful information.......
PCB Design Training in Chennai | PCB Training Institute in Velachery | PCB Courses in Velachery | Online Training Institute in Velachery

srihariparu said...

Your Post is really wonderful and amazing content.Thanks for sharing such a useful blog and really good...
Java Training Institute in Chennai | Java Training Center in Velachery | Online Training Institute in Velachery

deepti said...

This post is really nice and informative. The explanation given is really comprehensive and informative..
CCNA Training Institute in Chennai | CCNA Training Center in Velachery | CCNA Training Courses in Chennai | CCNA Training in Velachery | CCNA Online Training in Velachery

srihariparu said...

Thanks for giving nice information from your blog...It's really an amazing post...
Tally Training Institute in Chennai | Tally Training Center in Velachery | Tally Training with GST Training in Velachery | Online Training Center in Velachery

LinkWithin

Related Posts Plugin for WordPress, Blogger...