Java String Match regionMatches(String string, int toffset, StringBuffer other, int ooffset, int len)

Here you can find the source of regionMatches(String string, int toffset, StringBuffer other, int ooffset, int len)

Description

region Matches

License

Open Source License

Declaration

public static final boolean regionMatches(String string, int toffset, StringBuffer other, int ooffset,
            int len) 

Method Source Code

//package com.java2s;
/*//from  w w w . jav  a  2  s . co  m
 * This file is part of the Jose Project
 * see http://jose-chess.sourceforge.net/
 * (c) 2002-2006 Peter Sch?fer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

public class Main {
    /**
     * similar to the String.regionMatches but works on both String and StringBuffer
     */
    public static final boolean regionMatches(Object string, boolean ignoreCase, int toffset, Object other,
            int ooffset, int len) {
        if (string instanceof String) {
            if (other instanceof String)
                return ((String) string).regionMatches(ignoreCase, toffset, (String) other, ooffset, len);
            if (other instanceof StringBuffer) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((String) string, toffset, (StringBuffer) other, ooffset, len);
                else
                    return regionMatches((String) string, toffset, (StringBuffer) other, ooffset, len);
            }
        }
        if (string instanceof StringBuffer) {
            if (other instanceof String) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((StringBuffer) string, toffset, (String) other, ooffset, len);
                else
                    return regionMatches((StringBuffer) string, toffset, (String) other, ooffset, len);
            }
            if (other instanceof StringBuffer) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((StringBuffer) string, toffset, (StringBuffer) other, ooffset,
                            len);
                else
                    return regionMatches((StringBuffer) string, toffset, (StringBuffer) other, ooffset, len);
            }
        }
        if (string instanceof char[]) {
            if (other instanceof String) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((char[]) string, toffset, (String) other, ooffset, len);
                else
                    return regionMatches((char[]) string, toffset, (String) other, ooffset, len);
            } else
                throw new IllegalArgumentException("expecting String or StringBuffer");
        }
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }

    public static final boolean regionMatches(char[] string, int toffset, String other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length)
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string[toffset++] != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(String string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(StringBuffer string, int toffset, String other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(StringBuffer string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(String string, int toffset, char[] other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length)
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other[ooffset++])
                return false;
        }

        return true;
    }

    private static boolean regionMatchesIgnoreCase(char[] string, int toffset, String other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length)
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string[toffset++]) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(String string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(StringBuffer string, int toffset, String other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(StringBuffer string, int toffset, StringBuffer other,
            int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(String string, int toffset, char[] other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length)
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other[ooffset++]))
                return false;
        }

        return true;
    }

    public static final int length(Object string) {
        if (string instanceof String)
            return ((String) string).length();
        if (string instanceof StringBuffer)
            return ((StringBuffer) string).length();
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }
}

Related

  1. regionMatches(CharSequence receiver, int thisOffset, String other, int otherOffset, int length, boolean ignoreCase)
  2. regionMatches(CharSequence seq, int toff, CharSequence other, int ooff, int len)
  3. regionMatches(final char[] content, final int index, final String tag)
  4. regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart, final CharSequence substring, final int start, final int length)
  5. regionMatches(final CharSequence seq1, final int start1, final CharSequence seq2, final int start2, final int len, final boolean ignoreCase)
  6. regionMatchesIgnoreCase(char[] string, int toffset, String other, int ooffset, int len)