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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With