Java substring check

java, string

Solution

The obvious answer is `String1.contains(String2);`

It will throw a NullPointerException if `String1` is null. I would check that `String1` is not null before trying the comparison; the other situations should handle as you would expect.

Problem

I have a String2. I want to check whether String2 exists in String1. String1's length can be less or greater or equal than String2. Also String2 can be null or empty sometimes. How can I check this in my Java code?

Original source