0

I wanted to substring a String depending on int that I will passed on the method. I used nested loop for this. But everytime it loops I wanted to substring only from last substring to int the I passed in the method and get also the last string. How can I achieve this?

private static void input(String s, int I)
{
    List list = new ArrayList();

    for(int a = 0; a < s.length(); a++)
    {
        for(int position = 0; position < s.length(); position++)
        {
            if(position + a + I <= s.length())
            {
                list.add(s.substring(position, position + a + I));
            }
        }
    }

}

input("abaca", 2);

Expected output: "ab", "ac", "a"

6
  • in other words: you want to slice the input string into pieces of a length passed in? input("abaca", 2) does mean: slice "abaca" into substrings of length 2? Commented Jun 8, 2017 at 7:54
  • @Harmlezz Yes exactly like that. Commented Jun 8, 2017 at 7:54
  • See link below: Stackoverflow topic Commented Jun 8, 2017 at 7:56
  • A) dont use raw types B) dont use single character variable names. Names should tell the reader something about the purpose of the thing behind it. Your code is 10 times harder to understand, just for lazy naming. Commented Jun 8, 2017 at 7:56
  • @Oleg Estekhin duplicate? maybe, but look at the answer here to do it using regex one-liner! The old question wouldn't get attention to get such new brilliant answer! What about if over time there are new ways to do same things? How to improve, or somehow renew (to get new answers) such old questions on SO? Or maybe we should ask new? Commented Jun 8, 2017 at 8:55

3 Answers 3

2

You don't need nested loops. Just iterate over the String once, and add an I character substring in each iteration.

Note that a is the starting index of the current substring, and it therefore incremented by I in each iteration.

The last substring may have a shorter length. If a + I > s.length(), the last index of the last substring will be s.length() - 1 instead of a + I - 1.

for(int a = 0; a < s.length(); a+=I) {
    list.add(s.substring(a, Math.min(a + I, s.length())));
}

This produces

[ab, ac, a]

for input("abaca", 2).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. It's so simple and fast :)
2

You can also simply split the string:

private static void input(String s, int i){
    List list = Arrays.asList(s.split("(?<=\\G.{"+i+"})"));
    System.out.println(list);
}

\\G means The end of the previous match

?<= means positive lookbehind

Thanks to positive lookbehind (?<=) it will split on all zero length strings (without cutting off anything from the input string) preceded by where previous match ends (\\G) followed by i signs (.{"+i+"}).

2 Comments

WOW, this is something I was thinking about, great and very short
This looks new to me but it's readable. Thanks for this :)
0

It's really better to completely rewrite your code, but if you want to save your structure(even though it's not correct), just remove 1 loop and do something like this:

private static void input(String s, int I) {
    List list = new ArrayList();

    for (int position = 0; position < s.length();position += I) {
        if (position + I <= s.length()) {
            list.add(s.substring(position, position + I));
        } else {
            list.add(s.substring(position, s.length()));
            return;
        }
    }
}

1 Comment

of course it's better to user Eran solution!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.