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!

22 comments:

Srishthi B said...

Thank you. Found it interesting and useful. Java is a general purpose, high-level, class-based and object-oriented programming language. And we provide J2EE training in Chennai at Fita.

Jhon Mick said...

Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. Java Course in Chennai | Best JAVA Training in Chennai

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

for IT the said...

Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

for IT the said...

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training Java Online Training Java Online Training

Arthur Mac said...

Java is programming language which is used in almost all the applications and games which are on the web. Java is being used extensively and it will be used extensively in near future. So getting trained in Java will surely be helpful.
Thanks,
Java Training in Chennai | Java courses in Chennai | Java Training Institutes in Chennai

sunilkumarkuppam said...

The expansion of internet and other business intelligence leads to large volume of data. Industries are looking for talented professionals to maintain and process huge volume of data with latest tools available in the market. Taking Hadoop Training in Chennai | Big Data Training in Chennai will ensure better career prospects for talented professionals.

Evelin Harriet said...

I have read your blog its very Interesting. Thanks for sharing. ERP Providers in Chennai | ERP in Chennai

deepti said...

Excellent Post !!!!...
ITIL Training in Chennai | ITIL Certification Training in Chennai | ITIL Certification Exam Center in Chennai | No.1 ITIL Exam Center in Chennai

Caroline Keerth said...

Amazing blog about the various informative information of the programming languages. PHP is one such programming language which is more easier to learn and has lot of scope in future which is provided in Php Training institute in Chennai

Siva Seo said...

Nice Blog. Thank you for Sharing. Enterprise Resource Planning Software Solutions in Chennai. For more details info@bravetechnologies.in.
erp in Chennai | erp providers chennai

Shivani Yuvanjalin said...

Really Nice Blog. Thank you for Sharing. We are the best erp software providers in chennai. For more details call +91 9677025199 or email us on info@bravetechnologies.in.
ERP Software Providers in Chennai

Ajay Raj said...

Nice posting..

Red Hat Linux Training in Chennai
Red Hat Training in Chennai
Rhce Training in Chennai

anitha a said...

eagerly waiting for your new blog and useful information. keep doing more.
Web designing course in chennai

kumari said...

I am very happy reading your blog, it's really good and impressive content.. Selenium Training Institute in Chennai | Selenium Training Institute in Velachery.

Nandhini said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Java Training in Chennai

Nancy 12 said...

Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
Regards,
Java Training in Chennai

jahan bdreamz said...

Thanks for sharing this blog.It is really very impersive contant.Thanks for sharing.


Dot Net Training in chennai

Brave Technologies said...

Good post..Keep Sharing.! I'm working in brave technologies private limited. We are the leading ERP software development company in chennai.

sukere said...

Thanks for sharing impressive article…
Really I Enjoy this Blog…

Social Media Marketing
Digital Marketing
Bulk SMS Chennai

kalai hai said...

Nice to read such a awesome blog., Really this blog was so useful to IT Employees. Thank you for sharing this wonderful blog. CCNP Training in Chennai | CCNA Training in Chennai

Krishna Veni said...

Nice and good article, thanks for sharing your views and ideas, it is really useful to me.. Thanks , keep updating and rocks...
Best Dot Net Training in chennai | Dot Net Training institute in velachery

LinkWithin

Related Posts Plugin for WordPress, Blogger...