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!

36 comments:

brunagari said...


amazing
personalrekrutierung,

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

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

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

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

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

venusha 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

venusha said...

Excellent post... Thank you for sharing such a informative and information blog with us.keep updating such a wonderful post..
MicorSoft Azure Training Institute in Chennai | Azure Training Center in Chennai | Azure Certification Training in velachery | Online Azure training in Velachery

venusha said...

AllTechz Solutions provides real-time and placement focused Cisco CCNA training in Velachery Chennai . ATS’s CCNA certification course includes basic to advanced level and our Cisco CCNA course is designed to get the placement in good MNC companies in Chennai as quickly as once you complete the CCNA networking training course. ATS’s Cisco CCNA trainers are Cisco certified experts and experienced working professionals with hands on real time multiple CCNA projects knowledge. If you have any idea to join a job in the network industry,CCNA certification by CISCO will be the right choice. ATS’s CCNA Course in velachery will sharpen students knowledge about how to improve the productivity of the network and minimize network errors. CCNA also provide the candidates with a higher pay scale. So come and join CCNA training in Velachery.

venusha said...

AllTechz Solutions provides real-time and placement focused Cisco CCNA training in Velachery Chennai . ATS’s CCNA certification course includes basic to advanced level and our Cisco CCNA course is designed to get the placement in good MNC companies in Chennai as quickly as once you complete the CCNA networking training course. ATS’s Cisco CCNA trainers are Cisco certified experts and experienced working professionals with hands on real time multiple CCNA projects knowledge. If you have any idea to join a job in the network industry,CCNA certification by CISCO will be the right choice. ATS’s CCNA Course in velachery will sharpen students knowledge about how to improve the productivity of the network and minimize network errors. CCNA also provide the candidates with a higher pay scale. So come and join CCNA training in Velachery.

nandhini said...

“Superb!” Very Creative.... You have done really great job. It’s really useful to everyone .Thanks for share your kind information.
JAVA Training Institute in Velachery | JAVA Training Institute in Chennai

alexsamcurren said...

Thank you so much for sharing this worth able content with us. Keep blogging article like this.
Python Training in Chennai and Velachery |
AWS Training Center in Chennai & Velachery |
Java Training center in velachery|
Microsoft Azure Training Center in Chennai & Velachery |
Best ISTQB Exam center in velachery |

nandhini said...

Awesome post…It is really very interesting to read. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.


Python Training Institute in Velachery | Python Training Institute in Chennai

alexsamcurren 3 said...

Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it…
Web Designing and Development Training in velachery|
Microsoft Azure Training Center in Chennai & Velachery |
Best ISTQB Exam center in velachery |
Dot Net Trainig in Chennai and Velachery |
Python Training Center in Chennai and Velachery |
CCNA Training Center in Chennai & Velachery
Java Training center in velachery|

alexsamcurren 3 said...

Really I Enjoy this Blog…Very Nice Post…Thanks….
Dot Net Trainig in Chennai and Velachery |
Python Training Center in Chennai and Velachery |
CCNA Training Center in Chennai & Velachery
Java Training center in velachery|
Web Designing and Development Training in velachery|
Microsoft Azure Training Center in Chennai & Velachery |
Best ISTQB Exam center in velachery |

alexsamcurren 3 said...

It is a one of the great Explanation, which is very essential for me as well.
Web Designing and Development Training in velachery|
Dot Net Trainig in Chennai and Velachery |
Python Training Center in Chennai and Velachery |
CCNA Training Center in Chennai & Velachery
Java Training center in velachery|
Microsoft Azure Training Center in Chennai & Velachery |
Best ISTQB Exam center in velachery |

alexsamcurren 2 said...

Great article, your blog was really unique... thanks for sharing…
Selenium Training Center in Velachery | Tally Training center in velachery | Software Testing Training Center in Velachery | Web Designing and Development Training in velachery| Dot Net Trainig in Velachery | Python Training Center in Velachery | Java Training center in velachery|

alexsamcurren said...

Wonderful Article. Thank you for updating such an informative content.
Python Training Center in Velachery | Java Training center in velachery| Selenium Training Center in Velachery | Tally Training center in velachery | Software Testing Training Center in Velachery | Web Designing and Development Training in velachery| Dot Net Trainig in Velachery |

alexsamcurren 3 said...

This blog is really useful and it is very interesting thanks for sharing, it is really good and exclusive.
Tally Training center in velachery | Software Testing Training Center in Velachery | Web Designing and Development Training in velachery| Dot Net Trainig in Velachery | Python Training Center in Velachery | Java Training center in velachery| Selenium Training Center in Velachery |

alexsamcurren 3 said...

Impressive blog with lovely information. Really very useful article for us thanks for sharing such a wonderful blog...
Python Training Center in Velachery |
Java Training center in velachery|
Selenium Training Center in Velachery |
Tally Training center in velachery |
Software Testing Training Center in Velachery |
Web Designing and Development Training in velachery|
Dot Net Trainig in Velachery |

alexsamcurren 3 said...

Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...
JAVA Training in Chennai |
Summer Courses Training in Chennai|
Software Testing Training in Chennai |
ISTQB Training in Chennai |
Selenium Automation Training in Chennai |

alexsamcurren said...

Good information with unique content and it is very useful to know about the information based on blogs...
Python Training in Velachery|
CCNA Training in Velachery |
Dot Net Training in Velachery |
Hardware and Networking Training in Velachery |
Web Designing Training Center in Velachery
AWS Training in Velachery|

venusha said...

Nice post.. Its really an amazing with informative information and useful for everyone. Thanks for sharing your wonderful article..

AWS Certification in Chennai | AWS Exam Center in Chennai | AWS Exams in Velacheri | AWS Online Exams in Velachery | Online Certification in Chennai

alexsamcurren said...

Wonderful Article. Thank you for updating such an informative content.
Linux Training in chennai |
Dot Net Training in chennai |
AWS Training in chennai|
Certified Ethical Hacking Training in Chennai|
Hardware and Networking Training in chennai |
Python Training in chennai|
Web Designing Training Center in chennai

alexsamcurren 3 said...

It is a one of the great Explanation, which is very essential for me as well.
JAVA Training in chennai |
Python Training in chennai|
Dot Net Training in chennai |
Web Designing Training Center in chennai |
AWS Training in chennai|
Certified Ethical Hacking Training in Chennai|
Linux Training in chennai |
Hardware and Networking Training in chennai |

venusha said...

Amazing Information my sincere thanks for sharing this post Please Continue to share this kind of blog..

Final Year Project Center in Chennai | Final Year Projects in Velachery | Final Year IEEE Projects in Velachery | Online Project Center in Chennai | BE Projects in Chennai | ME Projects in Velachery

venusha said...

Really it was an awesome article...very interesting to read. You have provided an nice article....Thanks for sharing..
MBA Project Center in Chennai | MBA Projects in Velachery

Gayathri said...

Really nice blog.. Thanks for posting this interesting article with us
AWS Certification in Chennai | AWS Exam Center in Velachery

alexsamcurren said...

Interesting post. This is really helpful for me. I like it. Thanks for sharing.
JAVA Training in Chennai |
Python Training in Chennai|
AWS Training in Chennai|
Certified Ethical Hacking Training in Chennai|
Linux Training in Chennai |
Hardware and Networking Training in Chennai |
Dot Net Training in Chennai |
Web Designing Training Center in Chennai |

alexsamcurren said...

Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
Advanced & Core JAVA Training in Chennai |
Python Training in Chennai|
AWS Training in Chennai|
Certified Ethical Hacking Training in Chennai|
Linux Training in Chennai |

alexsamcurren said...

Nice Post. Thanks for sharing. Keep on updating.
Certified Ethical Hacking Training in Chennai & Velachery|
Linux Training in Chennai & Velachery |
Advanced & Core JAVA Training in Chennai & Velachery |
Python Training in Chennai & Velachery|
Hardware and Networking Training in Chennai & Velachery |
Dot Net Training in Chennai & Velachery |
Web Designing Training Center in Chennai & Velachery |
AWS Training in Chennai & Velachery|

venusha said...

Thank you so much for sharing your nice post with us.. keep updating..
GMAT Test Center in Chennai | GMAT Test Center in Velachery

alexsamcurren said...

Nice post... Really you are done a wonderful job. Thanks for sharing keep on updating...
Certified Ethical Hacking Training in Tambaram|
Linux Training in Tambaram |
AWS Training in Tambaram|
Advanced & Core JAVA Training in Tambaram |
Python Training in Tambaram|
Hardware and Networking Training in Tambaram |
Dot Net Training in Tambaram |
Web Designing Training Center in Tambaram |

alexsamcurren said...

Excellent information with Best content and it is very useful to know about the information based on blogs...
Certified Ethical Hacking Training in Velachery|
Linux Training in Velachery |
AWS Training in Velachery|
Advanced & Core JAVA Training in Velachery |
Python Training in Velachery|
Hardware and Networking Training in Velachery |
Dot Net Training in Velachery |
Web Designing Training Center in Velachery |

Mrbk30 said...

Very Informative blog thank you for sharing. Keep sharing.

Best software training institute in Chennai. Make your career development the best by learning software courses.

php training in chennai
uipath training in chennai
rpa training in chennai

LinkWithin

Related Posts Plugin for WordPress, Blogger...