7 Replies - 601 Views - Last Post: 14 August 2017 - 07:34 AM Rate Topic: -----

#1 Fluddershy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-June 16

Split string into equal chunks

Posted 13 August 2017 - 06:07 PM

Hello, 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));
    }

Thanks in advance, ~Fluddershy

This post has been edited by Fluddershy: 13 August 2017 - 06:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Split string into equal chunks

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,823
  • Joined: 25-December 13

Re: Split string into equal chunks

Posted 13 August 2017 - 06:27 PM

Is the problem supposed to preserve the words? IE No words can be split between chunks.
Is it always assumed that there is only one space between each pair of words?
What are the steps the code needs to take to solve the problem?
One of the steps would be split the String into its content words.
Then what?
Was This Post Helpful? 0
  • +
  • -

#3 Fluddershy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-June 16

Re: Split string into equal chunks

Posted 13 August 2017 - 06:44 PM

View PostNormR, on 13 August 2017 - 06:27 PM, said:

Is the problem supposed to preserve the words? IE No words can be split between chunks.
Is it always assumed that there is only one space between each pair of words?
What are the steps the code needs to take to solve the problem?
One of the steps would be split the String into its content words.
Then what?

Well, I plan on making this whole idea a method and just having a 1D array and adding that to my newMessage StringBuilder. To answer your second question, no, it just splits the whole String by the specified index no matter what. I'll provide another example:
"I love chicken", I'll input this into my method like so splitString("I love chicken", 3). It divides that by 3, and the output would be something like this:
System.out.println(getMessage[0]); (Would equal "I lo")
System.out.println(getMessage[1]); (Would equal "ve ch")
System.out.println(getMessage[2]); (Would equal "icken")
As you can see, since "I love chicken"'s length was 14, and divided by 3, it came out with 4.66 rounded, so I had to add 2 of the extra characters to index 1 and 2 of my 1D string array.

When you ask for what steps the code will take, I am not sure, which is why I am asking on this forum. I have took a couple tries, but there were a lot of ArrayIndexOutOfBoundsException's and it was just bad in general. If you would like to know what I've tried, I did make a char array from the string, got the length, divided it by the number, but got stuck.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 541
  • View blog
  • Posts: 2,197
  • Joined: 13-June 14

Re: Split string into equal chunks

Posted 13 August 2017 - 09:07 PM

View PostFluddershy, on 14 August 2017 - 02:44 AM, said:

When you ask for what steps the code will take, I am not sure


This is important. Programming is first and foremost a problem solving activity. Finding code on the Internet is not the way to go, for as you've found, you can often end up with code you don't understand. Another reason is that you'll be quite stuck if you can't find a solution to your problem. So, what should you do? Solve the problem by hand! In this case, write a string down on paper and work out how to get from there to the output you want. Imagine explaining the process to a 5 year old or someone. Once you have those steps, the code is much easier to write.
Was This Post Helpful? 0
  • +
  • -

#5 Arthur O Podd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 17

Re: Split string into equal chunks

Posted 14 August 2017 - 04:30 AM

I'd start off with the approach of taking the modulus of your string length and splitter length, and deciding what to do with the odd amount, if there is one.
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,823
  • Joined: 25-December 13

Re: Split string into equal chunks

Posted 14 August 2017 - 07:01 AM

Another similar problem to think about:
there is a pile of fruit (words) and some buckets (chunks).
You add pieces of fruit to the first bucket until when ???
before starting to add fruit to the second bucket.

This post has been edited by NormR: 14 August 2017 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Split string into equal chunks

Posted 14 August 2017 - 07:25 AM

Quote

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


In that case, you'd need to have something like

private List<StringChunk> split(String str, int chunks)


where the class encapsulates an index as well, wouldn't you?
Was This Post Helpful? 0
  • +
  • -

#8 Arthur O Podd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 17

Re: Split string into equal chunks

Posted 14 August 2017 - 07:34 AM

It should also be manageable via obtaining a String[] using the limited version of .split(). Look at that, as that will encapsulate indices at the same time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1