0

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));
}
Ragnarok
  • 3
  • 4
  • 1
    What's your question? – shmosel Aug 14 '17 at 04:44
  • I need help splitting a string into x amount equally. Like if I the input was 3, it would divide the string characters by x amount equally, so if I lined them up on 3 different rows, it would be similar, based on the amount of characters in the string. – Ragnarok Aug 14 '17 at 04:52
  • I understand the requirement, but I don't understand the question. Are you asking us to write code for you? To fix your code? – shmosel Aug 14 '17 at 04:53
  • Well, up to now, I've been trying to fix/make this for 3 days, as you have read with me researching it and all. I'm okay with you fixing the one I've found, writing it for me, or giving me detailed instructions on what to do. I'm sorry if I'm coming off as a beginner. I'm not too sure how to tackle this. :( – Ragnarok Aug 14 '17 at 04:56
  • What about the linked duplicate? Have you tried those solutions? – shmosel Aug 14 '17 at 04:59
  • That's not the same thing, they don't even know what I'm talking about. I even researched that question like 2 days ago and read over it, and didn't find my answer. – Ragnarok Aug 14 '17 at 05:03
  • You're being very vague. – shmosel Aug 14 '17 at 05:03
  • I don't know what else to do, I explained my question super extensively. What he marked as a duplicate was on how to limit the character split per x amount of characters. I want to just split it by the length of the string. – Ragnarok Aug 14 '17 at 05:05
  • It's the same basic requirement. You just need to establish the substring length as `str.length() / chunks`, as you already have in your snippet. – shmosel Aug 14 '17 at 05:08
  • But what would I do after that? I don't really even understand the snippet that well. :( – Ragnarok Aug 14 '17 at 05:11
  • Look at the answers in the posted duplicate - there are regex examples (replace 4 with 16) or more traditional ways using substring – Scary Wombat Aug 14 '17 at 06:49

0 Answers0