Does one string contain another, starting at a specific offset? - Android java.lang

Android examples for java.lang:String Search

Description

Does one string contain another, starting at a specific offset?

Demo Code

/*// www. j  av a2  s.  c  om
 *******************************************************************************
 * Copyright (C) 1996-2015, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */

public class Main{
    /**
     * Does one string contain another, starting at a specific offset?
     * @param text
     * @param offset
     * @param other
     * @return
     */
    public static int matchesAt(CharSequence text, int offset,
            CharSequence other) {
        int len = other.length();
        int i = 0;
        int j = offset;
        for (; i < len; ++i, ++j) {
            char pc = other.charAt(i);
            char tc = text.charAt(j);
            if (pc != tc)
                return -1;
        }
        return i;
    }
}

Related Tutorials