NeonMedusa
NeonMedusa

Reputation: 95

Check two words to see if the beginning of word a is equal to word b

Given the following strings

String a = "hello";
String b = "hell";
String c = "help";

I would like to find out if string b is a substring of a, and if string c is a substring of a.

You can see here the answer is clearly yes for b and no for c. How can I perform this test in Java?

Upvotes: 0

Views: 114

Answers (2)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

Try this:

if(a.contains(b)) {...}
if(a.contains(c)) {...}

works also with:

if(a.contains("ell")) {...}

Upvotes: 2

SLaks
SLaks

Reputation: 887453

Very simple:

if (a.startsWith(b))

Upvotes: 2

Related Questions