I have two strings like so:

String a = "on matt to";
String b = "onomatopoeia";

I've been trying to do...

if (a.contains(b)) return true;

It returns true in this case since the character sequence "mat" matches the "onomatopoeia".

I want it to return true only and only if the whole word onomatopoeia is in the String a followed or preceded by however many chars.

Can anyone help me?

Recommended Answers

All 3 Replies

You were already advised in your other thread about this to look at regex. I don't see any regex or mention of it here.

I wasn't able to figure it out.

I did some research and was under the impression if you put \s*stringContainingOnomatopoeia*\s then that would work so I tried it with a.matches(\s*stringContainingOnomatopoeia*\s) but that returned loads of errors. Even tried to do a.matches("\s*" + stringContainingOnomatopoeia + "*\s") and that still returned quite a few errors.

I have a feeling I just cant get the syntax right

> It returns true in this case since the character sequence "mat" matches the "onomatopoeia".

Really? Have you tried running the example you posted? If yes, post a working code which proves your point.

> I want it to return true only and only if the whole word onomatopoeia is in the String a followed or preceded by however many chars.

That's exactly how contains() works. http://ideone.com/LflVb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.