[Java] indexOf()、lastIndexOf()

indexOf( )
Code:
public static void main(String[] args)
{
String s1 = "ABCDEFG";

if(s1.indexOf("CDE")) //s1.indexOf("CDE") = 2
  System.out.println("CDE is located at index 2");

if(s1.indexOf("Test") < 0) //s1.indexOf("Test") = -1
  System.out.println("s1 not contain Test");
}

輸出:
CDE is located at index 2
s1 not contain Test

lastIndexOf( ) 
Code:
public static void main(String[] args)
{
String s1 = "ABCDEFG_ABCDEFG";

if(s1.lastIndexOf("CDE")) //s1.lastIndexOf("CDE") = 10
  System.out.println("CDE is located at index 10");

if(s1.lastIndexOf("Test") >= 0) //s1.lastIndexOf("Test") = -1
  System.out.println("s1 not contain Test");
}

輸出:
CDE is located at index 10
By Hao★

留言

熱門文章

[C#] 將DataTable轉換成Html格式表格

[MS SQL] 查詢所有Procedure