Java String Starts Wtih startsWith(String[] searchStrings, String text)

Here you can find the source of startsWith(String[] searchStrings, String text)

Description

Returns the index of the longest search string with which the given text starts or -1 if none matches.

License

Open Source License

Parameter

Parameter Description
searchStrings the strings to search for
text the text to search

Return

the index in searchStrings of the longest string with which text starts or -1

Declaration

public static int startsWith(String[] searchStrings, String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// w ww .j  a  v a 2 s.com
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Returns the index of the longest search string with which the given text starts or <code>-1</code>
     * if none matches.
     *
     * @param searchStrings the strings to search for
     * @param text the text to search
     * @return the index in <code>searchStrings</code> of the longest string with which <code>text</code> starts or <code>-1</code>
     */
    public static int startsWith(String[] searchStrings, String text) {

        int index = -1;

        for (int i = 0; i < searchStrings.length; i++) {
            if (text.startsWith(searchStrings[i])) {
                if (index == -1 || searchStrings[i].length() > searchStrings[index].length())
                    index = i;
            }
        }

        return index;
    }
}

Related

  1. startsWith(String text, String prefix, int toffset)
  2. startsWith(String toTest, String[] possibilities)
  3. startsWith(String value, String startsWith)
  4. startsWith(String[] array, String obj)
  5. startsWith(String[] beginningSegments, String[] testSegments)
  6. startsWith(String[] target, String[] with)
  7. startsWith(StringBuffer buffer, String prefix)
  8. startsWith(StringBuilder builder, String prefix, int offset)
  9. startsWith(StringBuilder sb, String prefix)