Java String Starts Wtih startsWith(String string, StringBuffer prefix, int toffset)

Here you can find the source of startsWith(String string, StringBuffer prefix, int toffset)

Description

starts With

License

Open Source License

Declaration

public static final boolean startsWith(String string, StringBuffer prefix, int toffset) 

Method Source Code

//package com.java2s;
/*/*from  w w w .j  a  v  a 2 s  .c om*/
 * 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 {
    public static final boolean startsWith(Object string, Object prefix, int toffset, boolean ignoreCase) {
        if (string instanceof String) {
            if (prefix instanceof String) {
                if (ignoreCase)
                    return startsWithIgnoreCase((String) string, (String) prefix, toffset);
                else
                    return ((String) string).startsWith((String) prefix, toffset);
            }
            if (prefix instanceof StringBuffer) {
                if (ignoreCase)
                    return startsWithIgnoreCase((String) string, (StringBuffer) prefix, toffset);
                else
                    return startsWith((String) string, (StringBuffer) prefix, toffset);
            }
        }
        if (string instanceof StringBuffer) {
            if (prefix instanceof String) {
                if (ignoreCase)
                    return startsWithIgnoreCase((StringBuffer) string, (String) prefix, toffset);
                else
                    return startsWith((StringBuffer) string, (String) prefix, toffset);
            }
            if (prefix instanceof StringBuffer) {
                if (ignoreCase)
                    return startsWithIgnoreCase((StringBuffer) string, (StringBuffer) prefix, toffset);
                else
                    return startsWith((StringBuffer) string, (StringBuffer) prefix, toffset);
            }
        }
        if (string instanceof char[]) {
            if (prefix instanceof String) {
                if (ignoreCase)
                    return startsWithIgnoreCase((char[]) string, (String) prefix, toffset);
                else
                    return startsWith((char[]) string, (String) prefix, toffset);
            } else
                throw new IllegalArgumentException("not implemented");
        }
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }

    public static final boolean startsWith(Object string, Object prefix, boolean ignoreCase) {
        return startsWith(string, prefix, 0, ignoreCase);
    }

    public static final boolean startsWith(String string, StringBuffer prefix, int toffset) {
        return regionMatches(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWith(StringBuffer string, String prefix, int toffset) {
        return regionMatches(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWith(StringBuffer string, StringBuffer prefix, int toffset) {
        return regionMatches(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWith(char[] string, String prefix, int toffset) {
        return regionMatches(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWith(String string, char[] other, int ooffset, int olen) {
        return regionMatches(string, 0, other, ooffset, olen);
    }

    public static final boolean startsWithIgnoreCase(Object string, Object prefix) {
        return startsWith(string, prefix, 0, true);
    }

    public static final boolean startsWithIgnoreCase(String string, String prefix, int toffset) {
        return string.regionMatches(true, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWithIgnoreCase(String string, StringBuffer prefix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWithIgnoreCase(StringBuffer string, String prefix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWithIgnoreCase(StringBuffer string, StringBuffer prefix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWithIgnoreCase(char[] string, String prefix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset, prefix, 0, prefix.length());
    }

    public static final boolean startsWithIgnoreCase(String string, char[] other, int ooffset, int olen) {
        return regionMatchesIgnoreCase(string, 0, other, ooffset, olen);
    }

    /**
     * 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;
    }

    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");
    }

    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;
    }
}

Related

  1. startsWith(String str, String prefix, int index)
  2. startsWith(String str, String start, boolean caseSensitive)
  3. startsWith(String str1Raw, String str2Raw)
  4. startsWith(String string, char... ca)
  5. startsWith(String string, String prefix)
  6. startsWith(String target, String... prefixes)
  7. startsWith(String text, String prefix, int toffset)
  8. startsWith(String toTest, String[] possibilities)
  9. startsWith(String value, String startsWith)