Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java substring check

Tags:

java

string

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?

like image 331
Arav Avatar asked Feb 22 '10 22:02

Arav


1 Answers

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.

like image 118
brabster Avatar answered Oct 02 '22 23:10

brabster