Java String Starts Wtih startsWith(CharSequence source, CharSequence search)

Here you can find the source of startsWith(CharSequence source, CharSequence search)

Description

Returns true if the source sequence starts with the search sequence.

License

Apache License

Declaration

public static boolean startsWith(CharSequence source, CharSequence search) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//from   w w  w. j  a v a 2  s.  co  m
     *  Returns true if the source sequence starts with the search sequence. This is
     *  simply a call to {@link #containsAt} with location 0.
     */
    public static boolean startsWith(CharSequence source, CharSequence search) {
        return containsAt(source, search, 0);
    }

    /**
     *  Returns true of the passed source sequence contains the specified search
     *  sequence starting at the given location. Will return false if either the
     *  source or search sequences are null, or the specified location is outside
     *  of the source sequence (either high or low).
     */
    public static boolean containsAt(CharSequence source, CharSequence search, int loc) {
        if (source == null)
            return false;
        if (search == null)
            return false;

        if (loc < 0)
            return false;
        if (loc + search.length() > source.length())
            return false;

        for (int ii = 0; ii < search.length(); ii++) {
            if (source.charAt(loc + ii) != search.charAt(ii))
                return false;
        }

        return true;
    }
}

Related

  1. startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive)
  2. startsWith(CharSequence input, String prefix)
  3. startsWith(CharSequence s, CharSequence seq)
  4. startsWith(CharSequence seq, char... any)
  5. startsWith(CharSequence seq, String str)
  6. startsWith(CharSequence str, char prefix)
  7. startsWith(CharSequence str, CharSequence prefix)
  8. startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)
  9. startsWith(final CharSequence str, final CharSequence prefix)