Java String Suffix suffix(final String source, final String target)

Here you can find the source of suffix(final String source, final String target)

Description

Find the length of a common suffix.

License

Open Source License

Parameter

Parameter Description
source First string
target Second string

Return

The number of characters common to the end of each string.

Declaration

public static int suffix(final String source, final String target) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  . j  a  v  a2 s .  c om*/
 * Distribution License:
 * JSword is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License, version 2.1 as published by
 * the Free Software Foundation. This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The License is available on the internet at:
 *       http://www.gnu.org/copyleft/lgpl.html
 * or by writing to:
 *      Free Software Foundation, Inc.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 * Copyright: 2007
 *     The copyright to this program is held by it's authors.
 *
 * ID: $Id$
 */

public class Main {
    /**
     * Find the length of a common suffix.
     * 
     * @param source
     *            First string
     * @param target
     *            Second string
     * @return The number of characters common to the end of each string.
     */
    public static int suffix(final String source, final String target) {
        int pointermin = 0;
        int pointermax = Math.min(source.length(), target.length());
        int pointermid = pointermax;
        while (pointermin < pointermid) {
            if (source.regionMatches(source.length() - pointermid, target, target.length() - pointermid,
                    pointermid)) {
                pointermin = pointermid;
            } else {
                pointermax = pointermid;
            }
            pointermid = (pointermax - pointermin) / 2 + pointermin;
        }

        return pointermid;
    }
}

Related

  1. suffix(String fname)
  2. suffix(String input, char delimiter)
  3. suffix(String mime)
  4. suffix(String name, char separator)