Q:

Java String separation using Substring

Here I have this phrase "THCUSATTYYE" and using the start index(USA) i would like to generate a string like this:
USATTYYE
SATTYYE
ATTYYE
I have this code so far :
public class StringCheck {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String phrase = "THCUSATTYYE";

        int useThisStart = phrase.indexOf("USA");
        String word =  new String ("");
        String word2 = new String ("");
        String word3 = new String ("");
        //First Loop
        for (int k = 3; k < phrase.length() - useThisStart-2; k += 3) {
            word =phrase.substring(useThisStart + k, useThisStart + k + 3);
        }
        //Secod Loop
        for (int k = 3; k < phrase.length() - useThisStart-2; k += 3) {
            word2 =phrase.substring(useThisStart + k + 1, useThisStart + k + 3 +1);
        }
        //Third Loop
        for (int k = 3; k < phrase.length() - useThisStart-2; k += 3) {
            word3 =phrase.substring(useThisStart + k + 2, useThisStart + k + 3+ 2);
        }
        System.out.println(word);
        System.out.println(word2);
        System.out.println(word3);
    }

}

but generates this output:
TTY
TYY
YYE
How can I generate this output below from my code sample:
USATTYYE
SATTYYE
ATTYYE 

A:

You can do it with one variable and a loop, e.g.:
public static void main(String[] args) throws Exception {
    String input = "THCUSATTYYE";
    String token = "USA";
    int index = input.indexOf(token);
    if(index != -1){
        String remainder = input.substring(index + token.length());
        for(int i=0 ; i<token.length() ; i++){
            System.out.println(token.substring(i) + remainder);
        }
    }
}

A:

First, test that your phrase was found. Then use one loop, for a total of 3 iterations, starting at your initial position. Print the substring from the start index. Like,
String phrase = "THCUSATTYYE";
int useThisStart = phrase.indexOf("USA");
if (useThisStart > -1) {
    for (int i = useThisStart; i < useThisStart + 3; i++) {
        System.out.println(phrase.substring(i));
    }
}

Outputs (as requested)
USATTYYE
SATTYYE
ATTYYE

Or, in Java 8+, you could use an IntStream.range like
if (useThisStart > -1) {
    IntStream.range(useThisStart, useThisStart + 3)
            .forEachOrdered(i -> System.out.println(phrase.substring(i)));
}

