CharSequence starts With - Java java.lang

Java examples for java.lang:CharSequence

Description

CharSequence starts With

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        CharSequence string = "java2s.com";
        CharSequence startsWith = "java2s.com";
        System.out.println(startsWith(string, startsWith));
    }//from w w w  .  jav a2s  .c  o m

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

        if (string.length() < startsWith.length()) {
            return false;
        }

        if (startsWith.length() == 0) {
            return false;
        }

        int max = startsWith.length();
        for (int i = 0; i < max; i++) {
            if (string.charAt(i) != startsWith.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials