Java文字列の検索

2946 ワード

int indexOf(int ch)は、指定した文字が最初に出現した文字列内のインデックスを返します.インデックスは0から始まり、対応する位置を返します.注意:見つからない場合は-1を返します.以下同.
		String url = "https://blog.csdn.net/Butterfly_resting";
        int index= url.indexOf("/");
        System.out.println("index = " + index);
       //	index = 6  

int indexOf(int ch,int fromIndex)は、指定した文字が最初に出現した文字列内のインデックスを返し、指定したインデックスで検索を開始します.
		//  5   ,     c url   
		String url = "https://blog.csdn.net/Butterfly_resting";
        int index = url.indexOf('c',4);
        System.out.println("index = " + index);

		//index = 13

int indexOf(String str)は、指定されたサブ文字列が初めて現れる文字列内のインデックスを返します.
		//     Butterfly_resting   
		String url = "https://blog.csdn.net/Butterfly_resting";
		 int index = url.indexOf("Butterfly_resting");
        System.out.println("index = " + index);
		//index = 22

int indexOf(String str,int fromIndex)は、指定されたサブストリングの最初に発生した文字列のインデックスを返し、指定されたインデックスから開始します.
		//  5   ,      tt url   
		String url = "https://blog.csdn.net/Butterfly_resting";
		int index = url.indexOf("tt",5);
        System.out.println("index = " + index);
		//index = 24

int lastIndexOf(int ch)は、指定した文字の最後に出現した文字列のインデックスを返します.
	String url = "https://blog.csdn.net/Butterfly_resting";
	int lastIndex = url.lastIndexOf('/');
    System.out.println("lastIndex = " + lastIndex);
    //lastIndex = 21

int lastIndexOf(int ch,int fromIndex)は、指定した文字の最後に表示された文字列のインデックスを返し、指定したインデックスから後へ検索します.【位置を指定して、左文字列で探します】
	//
	String url = "https://blog.csdn.net/Butterfly_resting";
	int lastIndex = url.lastIndexOf("/",10);
    System.out.println("lastIndex = " + lastIndex);
    //lastIndex = 7

int lastIndexOf(String str)は、指定したサブ文字列が最後に表示された文字列のインデックスを返します.
	String url = "https://blog.csdn.net/Butterfly_resting";
	int lastIndex = url.lastIndexOf("tt");
    System.out.println("lastIndex = " + lastIndex);
    //lastIndex = 24

int lastIndexOf(String str,int fromIndex)は、指定されたサブ文字列の最後に表示された文字列のインデックスを返し、指定されたインデックスから後方検索を開始します.
	//  5   ,           tt url   
	String url = "https://blog.csdn.net/Butterfly_resting";
	int lastIndex = url.lastIndexOf("tt",5);
    System.out.println("lastIndex = " + lastIndex);
	//lastIndex = 1

上の位置マッチングはsubstringメソッドと文字列を切り取ることができます.String substring(int beginIndex)は、この文字列のサブ文字列である文字列を返します.String substring(int beginIndex,int endIndex)は、この文字列のサブ文字列である文字列を返します.
例:
		//  url     
		String url = "/blog.csdn.net/Butterfly_resting?status=1";
        int index = url.indexOf('?');
        System.out.println(url.substring(index));
        //status=1