Bravo Bravo - 1 year ago 33
Java Question

Checking for words with prefix & suffix

I tried but it seems I couldn't find out. The base form of word is "pakai" and the modified word (with prefix) is "memakai".

The prefix is "me" but when it gets combined to "pakai", it turns the first alphabet "p" to "m", so "pakai" turns to "makai".

So pakai --> memakai.

String w1 = "memakai";

String w2 = "pakai";

I tried

k1.contains(k2) --> false, it's correct since there is no "p" in "memakai"
k1.indexOf(k2) --> -1

My question is how to check if w1 contains in w2 (w2 is base form of w1). Is there any efficient way to solve this case. Thanks


Try this :

boolean retVal;
retVal = K1.endsWith( K2.substring(1) );
System.out.println("Returned Value = " + retVal );

If you don't know the starting index for .substring(StsrtIndex) use this :

boolean retVal=Boolean.FALSE;
for(int i=0; i<(K2.length()-1);i++)
    retVal = K1.endsWith( K2.substring(i) );
System.out.println("Returned Value = " + retVal );