Java String Starts Wtih startsWithIgnoreWhitespaces(String prefix, String string)

Here you can find the source of startsWithIgnoreWhitespaces(String prefix, String string)

Description

Tests if this string starts with the specified prefix (Ignoring whitespaces)

License

Apache License

Parameter

Parameter Description
prefix a parameter
string a parameter

Return

boolean

Declaration

public static boolean startsWithIgnoreWhitespaces(String prefix, String string) 

Method Source Code

//package com.java2s;
/*/*  w ww.j a va 2 s.  c o  m*/
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Tests if this string starts with the specified prefix (Ignoring whitespaces)
     *
     * @param prefix
     * @param string
     * @return boolean
     */
    public static boolean startsWithIgnoreWhitespaces(String prefix, String string) {
        int index1 = 0;
        int index2 = 0;
        int length1 = prefix.length();
        int length2 = string.length();
        char ch1 = ' ';
        char ch2 = ' ';
        while (index1 < length1 && index2 < length2) {
            while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) {
                index1++;
            }
            while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) {
                index2++;
            }
            if (index1 == length1 && index2 == length2) {
                return true;
            }
            if (ch1 != ch2) {
                return false;
            }
            index1++;
            index2++;
        }

        if (index1 < length1 && index2 >= length2) {
            return false;
        }

        return true;
    }
}

Related

  1. startsWithIgnoreCase(String[] compoundName, String[] prefix)
  2. startsWithIgnoreCaseAndNonAlphaNumeric(String searchIn, String searchFor)
  3. startsWithIgnoreCaseAndWs(String searchIn, String searchFor)
  4. startsWithIgnoreCaseAndWs(String searchIn, String searchFor, int beginPos)
  5. startsWithIgnoreWhitespace(String str, String prefix)
  6. startsWithIndex(String[] prefixes, String fullPath)
  7. startsWithLenient(final String s, final String[] matches, final int[] minChars, final boolean acceptTrailing)
  8. startsWithLetter(String str)
  9. startsWithLetterOrUnderscore(String value)