Java String Starts Wtih startsWith(CharSequence str, char prefix)

Here you can find the source of startsWith(CharSequence str, char prefix)

Description

starts With

License

MIT License

Declaration

public static boolean startsWith(CharSequence str, char prefix) 

Method Source Code

//package com.java2s;
/*!/*from w ww .j a  va 2s  .  co  m*/
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

public class Main {
    public static boolean startsWith(CharSequence str, char prefix) {
        if (str == null) {
            return false;
        }

        if (str.length() < 1) {
            return false;
        }

        char firstCh = str.charAt(0);
        return (firstCh == prefix);
    }

    public static boolean startsWith(CharSequence str, CharSequence prefix) {
        if (str == null) {
            return false;
        }

        if (prefix == null || prefix.length() == 0) {
            return true;
        }

        int len = prefix.length();

        if (str.length() < len) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            char prefixCh = prefix.charAt(i);

            if (ch != prefixCh) {
                return false;
            }
        }

        return true;
    }

    public static CharSequence startsWith(CharSequence str, CharSequence... prefixes) {
        if (str == null) {
            return null;
        }

        if (prefixes == null || prefixes.length == 0) {
            return null;
        }

        for (CharSequence prefix : prefixes) {
            if (startsWith(str, prefix)) {
                return prefix;
            }
        }

        return null;
    }
}

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)
  5. startsWith(CharSequence source, CharSequence search)
  6. startsWith(CharSequence str, CharSequence prefix)
  7. startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)
  8. startsWith(final CharSequence str, final CharSequence prefix)
  9. startsWith(final CharSequence target, final CharSequence prefix)