简体   繁体   中英

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

You can do it with one variable and a loop, eg:

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

You don't need to use for loops for that, you can do it like that:

word = phrase.substring(useThisStart, phrase.length())

and then respectively:

word2 = phrase.substring(useThisStart+1, phrase.length())

word3 = phrase.substring(useThisStart+2, phrase.length())

You needn't give this method an end index, in this case you can use it without it.

You could something like this

public static void main(String[] args) {
    String phrase = "THCUSATTYYE";
    String wordOfChoice = "USA";
    int wordOfChoiceCombos = wordOfChoice.length();
    int useThisStart = phrase.indexOf(wordOfChoice);

    while(wordOfChoiceCombos-- > 0 && useThisStart != -1)
         System.out.println(phrase.substring(useThisStart++));

}

Output

USATTYYE
SATTYYE
ATTYYE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM