and I am having a real hard time figuring this out. I need to split a string into equal length with an index for it perferably. I've been researching and trying stuff out on this topic for the past 2 days and found nothing. I don't want to waste any more of my or your time so I'll show you want I need. This is an example of what I'm trying to do.
Jake says "Hello there, I'm a nice guy who climbs on rocks.", and that sentence was 47 characters long, so I assumed I'd need to divide that by the index I want to split it. So then say I'd divide that by 3, which is 16 rounded up. So it would be displayed like this: "Hello there, I'm", " a nice guy who ", "climbs on rocks.", but in other cases it wouldn't always be even. In attempt to try and help you even further, I have some C# code I found online, I converted most of it to Java but the bottom part, due to all the confusing lambdas in there and such near the bottom. Here it is:
private List<String> split(String str, int chunks) {
List<String> l = new ArrayList<>();
if (str == null || str.isEmpty())
return l;
if (str.length() < chunks) {
l.add(str);
return l;
}
int chunkSize = str.length() / chunks;
int stringLength = str.length();
for (int i = 0; i < stringLength; i += chunkSize) {
if (i + chunkSize > stringLength) {
chunkSize = stringLength - i;
l.add(str.substring(i, chunkSize));
}
}
final String[] residual = {""};
l.set((f, i) -> (i > chunks - 1).ToList().ForEach(f -> residual[0] += f));
l.set(chunks - 1, l.get(chunks - 1) + residual[0]);
return Arrays.asList(l.get(chunks));
}