Search code examples
javastringkeyphrase

Java: Output String Containing a Key Phrase


I am hoping to understand how to extract words from a string that contain a key phrase.

Let's assume that the key phrase is "_01", what is a good method for outputting any word in the input sequence "panorder_01, panorder_02, disorder_01, forkorder_01, forkorder_02, forkorder_03" that contains the key phrase?


Solution

  • You can split first your string and use the contains method to check of the string contains "_01"

    sample:

    String s = "panorder_01, panorder_02, disorder_01, forkorder_01, forkorder_02, forkorder_03";
        String s2 [] = s.split(",");
        for(String s3 : s2)
        {
            if(s3.trim().contains("_01"))
                System.out.println(s3.trim());
        }
    

    result:

    panorder_01
    disorder_01
    forkorder_01