Get index Of one substring inside the long string - Android java.lang

Android examples for java.lang:String Substring

Description

Get index Of one substring inside the long string

Demo Code

import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/*  w  w w  .java 2  s. co  m*/
     * <pre>
     * StringUtil.indexOf(null, *) = -1
     * StringUtil.indexOf(*, null) = -1
     * StringUtil.indexOf("", "") = 0
     * StringUtil.indexOf("aabaabaa", "a") = 0
     * StringUtil.indexOf("aabaabaa", "b") = 2
     * StringUtil.indexOf("aabaabaa", "ab") = 1
     * StringUtil.indexOf("aabaabaa", "") = 0
     * </pre>
     * 
     */
    public static int indexOf(String str, String searchStr) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }

        return str.indexOf(searchStr);
    }
    /**
     * <pre>
     * StringUtil.indexOf(null, *, *) = -1
     * StringUtil.indexOf(*, null, *) = -1
     * StringUtil.indexOf("", "", 0) = 0
     * StringUtil.indexOf("aabaabaa", "a", 0) = 0
     * StringUtil.indexOf("aabaabaa", "b", 0) = 2
     * StringUtil.indexOf("aabaabaa", "ab", 0) = 1
     * StringUtil.indexOf("aabaabaa", "b", 3) = 5
     * StringUtil.indexOf("aabaabaa", "b", 9) = -1
     * StringUtil.indexOf("aabaabaa", "b", -1) = 2
     * StringUtil.indexOf("aabaabaa", "", 2) = 2
     * StringUtil.indexOf("abc", "", 9) = 3
     * </pre>
     */
    public static int indexOf(String str, String searchStr, int startPos) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }

        if ((searchStr.length() == 0) && (startPos >= str.length())) {
            return str.length();
        }

        return str.indexOf(searchStr, startPos);
    }

}

Related Tutorials