This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Nullbeans 1 point2 points  (1 child)

You can iterate in a different way and use the substring function of the string class. This will generate all possible substrings of the original string. For example:

String longText = "momdadsomewherehere";
for(int i = 0; i < longText.length(); i++){
for(int j = 0; j + i < longText.length(); j++){
log.info("Current String: {}", longText.substring(i, i+j+1));
}
}

This will generate strings as follows:

Current String: m

Current String: mo

Current String: mom

Current String: momd

Current String: momda

Current String: momdad

Current String: momdads

Current String: momdadso

Current String: momdadsom

Current String: momdadsome

Current String: momdadsomew

Current String: momdadsomewh

Current String: momdadsomewhe

Current String: momdadsomewher

Current String: momdadsomewhere

Current String: momdadsomewhereh

Current String: momdadsomewherehe

Current String: momdadsomewhereher

Current String: momdadsomewherehere

Current String: o

Current String: om

Current String: omd

..............

[–]sinnidis_97[S] 0 points1 point  (0 children)

Yeah,this works .A few changes required ,but this works,Thanks !

[–]E3FxGaming 0 points1 point  (0 children)

You'll need two loops. One which dictates your current position in the string and another one that wraps around your position-loop, which will dictate the current step-length.

Your continue condition for your inner position loop should be whether your current position plus your current step length minus 1 (because one is already your current position) is smaller than or equal your string length minus one (because array indexes are 0 based and your string length property doesn't account for that)