-3

if String a = "ahmad"; and String b = "ahmadkhan"; then, how to check that ahmad is present in ahmadkhan in start or in last. That is, how to check whether String a is there in String b ?

Lal
  • 14,726
  • 4
  • 45
  • 70
Aasim
  • 15
  • 2
  • possible duplicate of [Is the Contains Method in java.lang.String Case-sensitive?](http://stackoverflow.com/questions/86780/is-the-contains-method-in-java-lang-string-case-sensitive) – N J May 09 '15 at 17:43

3 Answers3

4

You can use startsWith and endsWith to check whether some string starts or ends with another one.

boolean containsString = b.startsWith(a) || b.endsWith(a); 
if (containsString) {
// do something
}
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • And what happens if a is in b, but b does not start and does not end with a. – Lajos Arpad May 09 '15 at 18:30
  • @LajosArpad Original question (it has been edited now and some intent has been lost) asked how to check whether string `b` has string `a` at start or last (end). At least that is how I understood the question. It does not ask how to check if string `b` contains string `a` anywhere. – Dalija Prasnikar May 09 '15 at 19:05
0

You can use contains

If String a = "ahmad"; and String b = "ahmadkhan"; , then you can check whether ahmad is present in ahmadkhan like

if(b.contains(a))
{
    //do your code
}
Lal
  • 14,726
  • 4
  • 45
  • 70
0
b.indexOf(a) >= 0

is the criteria to be checked.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175