Java String Starts Wtih startsWith(String s, char c)

Here you can find the source of startsWith(String s, char c)

Description

An efficient method for checking if a string starts with a character.

License

Apache License

Parameter

Parameter Description
s The string to check. Can be <jk>null</jk>.
c The character to check for.

Return

true if the specified string is not null and starts with the specified character.

Declaration

public static boolean startsWith(String s, char c) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

public class Main {
    /**/*w  w  w . java 2  s .c  o  m*/
     * An efficient method for checking if a string starts with a character.
     *
     * @param s The string to check.  Can be <jk>null</jk>.
     * @param c The character to check for.
     * @return <jk>true</jk> if the specified string is not <jk>null</jk> and starts with the specified character.
     */
    public static boolean startsWith(String s, char c) {
        if (s != null) {
            int i = s.length();
            if (i > 0)
                return s.charAt(0) == c;
        }
        return false;
    }

    /**
     * Returns the character at the specified index in the string without throwing exceptions.
     *
     * @param s The string.
     * @param i The index position.
     * @return The character at the specified index, or <code>0</code> if the index is out-of-range or the string
     *    is <jk>null</jk>.
     */
    public static char charAt(String s, int i) {
        if (s == null)
            return 0;
        if (i < 0 || i >= s.length())
            return 0;
        return s.charAt(i);
    }
}

Related

  1. startsWith(String path, String prefix)
  2. startsWith(String prefix, String string, boolean caseInsensitive)
  3. startsWith(String receiver, String... needles)
  4. startsWith(String s, boolean caseIgnore, String... args)
  5. startsWith(String s, char begin)
  6. startsWith(String s, int offset, int end, String t)
  7. startsWith(String s, String begin)
  8. startswith(String s, String prefix)
  9. startsWith(String s, String prefix)