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!

23 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...

Really nice and good post. Thank you for sharing amazing information.
PHP Project Center in Chennai | PHP Projects in Velachery | PHP Project Center in Nanganallur | PHP Projects in Guindy | PHP projects in Taramani

deepti said...

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

deepti said...

I read this article. I think You put a lot of effort to create this article. I appreciate your work.
Embedded System Training Institute in Chennai | Embedded Training Center in Velachery | Embedded Training in Guindy

LinkWithin

Related Posts Plugin for WordPress, Blogger...