Home / Programs / Write a Java program to find whether a region in the current string matches a region in another string.
🚀 Programming Example

Write a Java program to find whether a region in the current string matches a region in another string.

👁 1,332 Views
💻 Practical Program
📘 Step Learning
Java program to find whether a region in the current string matches a region in another string.

💻 Program Code

public class Exercise {

  public static void main(String[] args)
    {
       //String str1 = "Red Green Orange Yellow";
        //String str2 = "Yellow Orange Green Red";

        String str1 = "Shanghai Houston Colorado Alexandria";
        String str2 = "Alexandria Colorado Houston Shanghai";

        // Determine whether characters 0 through 7 in str1
        // match characters 28 through 35 in str2.
        boolean match1 = str1.regionMatches(0, str2, 28, 8);

        // Determine whether characters 9 through 15 in str1
        // match characters 9 through 15 in str2.
        boolean match2 = str1.regionMatches(9, str2, 9, 8);

        // Display the results of the regionMatches method calls.
        System.out.println("str1[0 - 7] == str2[28 - 35]? " + match1);
        System.out.println("str1[9 - 15] == str2[9 - 15]? " + match2);
    }
}

                        

🖥 Program Output

str1[0 - 7] == str2[28 - 35]? true
str1[9 - 15] == str2[9 - 15]? false
Press any key to continue . . .
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.