Java String Starts Wtih startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive)

Here you can find the source of startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive)

Description

Tests if a char sequence starts with some prefix

License

Open Source License

Parameter

Parameter Description
cs the char sequence to test
prefix the prefix to test
caseSensitive do case sensitive comparison?

Return

true if cs starts with prefix

Declaration

public static boolean startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive) 

Method Source Code

//package com.java2s;

public class Main {
    /** Tests if a char sequence starts with some prefix
     * @param   cs      the char sequence to test
     * @param   prefix   the prefix to test
     * @return true if cs starts with prefix
     *//*w  w w .ja va2  s .co  m*/
    public static boolean startsWith(CharSequence cs, CharSequence prefix) {
        return startsWith(cs, prefix, 0, true);
    }

    /** Tests if a char sequence starts with some prefix
     * @param   cs            the char sequence to test
     * @param   prefix         the prefix to test
     * @param   caseSensitive   do case sensitive comparison?
     * @return true if cs starts with prefix
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive) {
        return startsWith(cs, prefix, 0, caseSensitive);
    }

    /** Tests if a char sequence starts with some prefix at a specific offset
     * @param   cs      the char sequence to test
     * @param   prefix   the prefix to test
     * @param   offset   the offset to look at
     * @return true if prefix lies in cs, starting at the specified offset 
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, int offset) {
        return startsWith(cs, prefix, offset, true);
    }

    /** Tests if a char sequence starts with some prefix at a specific offset
     * @param   cs            the char sequence to test
     * @param   prefix         the prefix to test
     * @param   offset         the offset to look at
     * @param   caseSensitive   do case sensitive comparison?
     * @return true if prefix lies in cs, starting at the specified offset 
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, int offset, boolean caseSensitive) {
        if (offset < 0)
            return false;
        int prefixLength = prefix.length();
        if (cs.length() < (offset + prefixLength))
            return false;
        int c;
        for (c = 0; (c < prefixLength) && ((c + offset) < cs.length()); c++)
            if (caseSensitive && cs.charAt(offset + c) != prefix.charAt(c))
                return false;
            else if (!caseSensitive
                    && Character.toLowerCase(cs.charAt(offset + c)) != Character.toLowerCase(prefix.charAt(c)))
                return false;
        return (c == prefixLength);
    }
}

Related

  1. startsWith(CharSequence input, String prefix)
  2. startsWith(CharSequence s, CharSequence seq)
  3. startsWith(CharSequence seq, char... any)
  4. startsWith(CharSequence seq, String str)