Java String Starts Wtih startsWithWeight(String s1, String s2)

Here you can find the source of startsWithWeight(String s1, String s2)

Description

Return the number of starting letters that s1 and s2 have in common before they deviate.

License

Open Source License

Return

the number of starting letters that s1 and s2 have in common before they deviate

Declaration

public static int startsWithWeight(String s1, String s2) 

Method Source Code

//package com.java2s;
/*// ww  w.j  a  v a 2 s  . co m
 * Copyright (C) 2003-2011 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Return the number of starting letters that s1 and s2 have in common
     * before they deviate.
     *
     * @return the number of starting letters that s1 and s2 have in common
     *         before they deviate
     */
    public static int startsWithWeight(String s1, String s2) {
        if ((s1 == null) || (s2 == null)) {
            return 0;
        }

        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();

        int i = 0;

        for (; (i < chars1.length) && (i < chars2.length); i++) {
            if (chars1[i] != chars2[i]) {
                break;
            }
        }

        return i;
    }
}

Related

  1. startsWithURIScheme(String arg)
  2. startsWithVowel(String string)
  3. startsWithVowel(String text)
  4. startsWithVowel(String value)
  5. startsWithVowel(String word)
  6. startsWithWhitespace(final CharSequence charSeq)
  7. startsWithWhitespace(String s)
  8. startsWithWithoutBeingFollowedByLetter(String s, String compareTo)
  9. startsWithWovel(String s)