Java String Starts Wtih startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)

Here you can find the source of startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)

Description

Checks whether a text starts with a specified prefix.

License

Apache License

Parameter

Parameter Description
caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
text the text to be checked for prefixes.
prefix the prefix to be searched.

Return

whether the text starts with the prefix or not.

Declaration

public static boolean startsWith(final boolean caseSensitive, final char[] text, final char[] prefix) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .  j a  va 2  s .  com*/
 * =============================================================================
 * 
 *   Copyright (c) 2012-2014, The ATTOPARSER team (http://www.attoparser.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */

public class Main {
    /**
     * <p>
     *   Checks whether a text starts with a specified prefix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param prefix the prefix to be searched.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final String text, final String prefix) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (prefix == null) {
            throw new IllegalArgumentException("Prefix cannot be null");
        }

        return (caseSensitive ? text.startsWith(prefix)
                : startsWith(caseSensitive, text, 0, text.length(), prefix, 0, prefix.length()));

    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param prefix the prefix to be searched.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final String text, final char[] prefix) {
        return startsWith(caseSensitive, text, 0, text.length(), prefix, 0, prefix.length);
    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param prefix the prefix to be searched.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final char[] text, final char[] prefix) {
        return startsWith(caseSensitive, text, 0, text.length, prefix, 0, prefix.length);
    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param prefix the prefix to be searched.
     * @param prefixOffset the offset of the prefix.
     * @param prefixLen the length of the prefix.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final char[] text, final int textOffset,
            final int textLen, final char[] prefix, final int prefixOffset, final int prefixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (prefix == null) {
            throw new IllegalArgumentException("Prefix cannot be null");
        }

        if (textLen < prefixLen) {
            return false;
        }

        char c1, c2;

        int n = prefixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text[textOffset + i];
            c2 = prefix[prefixOffset + i];

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param prefix the prefix to be searched.
     * @param prefixOffset the offset of the prefix.
     * @param prefixLen the length of the prefix.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final String text, final int textOffset,
            final int textLen, final char[] prefix, final int prefixOffset, final int prefixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (prefix == null) {
            throw new IllegalArgumentException("Prefix cannot be null");
        }

        if (textLen < prefixLen) {
            return false;
        }

        char c1, c2;

        int n = prefixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text.charAt(textOffset + i);
            c2 = prefix[prefixOffset + i];

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param prefix the prefix to be searched.
     * @param prefixOffset the offset of the prefix.
     * @param prefixLen the length of the prefix.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final char[] text, final int textOffset,
            final int textLen, final String prefix, final int prefixOffset, final int prefixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (prefix == null) {
            throw new IllegalArgumentException("Prefix cannot be null");
        }

        if (textLen < prefixLen) {
            return false;
        }

        char c1, c2;

        int n = prefixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text[textOffset + i];
            c2 = prefix.charAt(prefixOffset + i);

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text starts with a specified prefix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for prefixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param prefix the prefix to be searched.
     * @param prefixOffset the offset of the prefix.
     * @param prefixLen the length of the prefix.
     * @return whether the text starts with the prefix or not.
     */
    public static boolean startsWith(final boolean caseSensitive, final String text, final int textOffset,
            final int textLen, final String prefix, final int prefixOffset, final int prefixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (prefix == null) {
            throw new IllegalArgumentException("Prefix cannot be null");
        }

        if (textLen < prefixLen) {
            return false;
        }

        char c1, c2;

        int n = prefixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text.charAt(textOffset + i);
            c2 = prefix.charAt(prefixOffset + i);

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }
}

Related

  1. startsWith(CharSequence seq, char... any)
  2. startsWith(CharSequence seq, String str)
  3. startsWith(CharSequence source, CharSequence search)
  4. startsWith(CharSequence str, char prefix)
  5. startsWith(CharSequence str, CharSequence prefix)
  6. startsWith(final CharSequence str, final CharSequence prefix)
  7. startsWith(final CharSequence target, final CharSequence prefix)
  8. startsWith(final E[] array, final E[] prefix)
  9. startsWith(final Object[] left, final Object[] right, final boolean equals)