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 == chunkLengthThe 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!
8 comments:
Really nice post.Thanks for sharing.
jobbörse siegerland
Your website content nice nice and interesting to observe.
jobbörse Neunkirchen
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
python training in Chennai
really good
Jobvermittlung
amazing
personalrekrutierung,
Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Pega Training in Chennai
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
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
Post a Comment