Example 1:
String. indexof (string X): The index of the following table that appears for the first time in the string. If "X" does not exist,-1 is returned.
String. substring (INT beginindex): It is truncated from the string index to beginindex, including the index
String STR = "Shaanxi Province | Xi'an | Yanta district"; system. out. println (Str. indexof ("|"); // output 3system. out. println (Str. substring (Str. indexof ("|") + 1); // The output is "Xi'an | Yanta district". Remove "Shaanxi Province | ".
Example 2:
String STR = "ABC. ABC. AAA "; system. out. println ("'. 'Index of the first position in the STR string: "+ Str. indexof (". "); system. out. println ("truncated ABC value" + Str. substring (0, str. indexof (". "); system. out. println ("truncates the value of ABC in the middle:" + Str. substring (Str. indexof (". ") + 1, str. lastindexof (". "); system. out. println ("truncated AAA value:" + Str. substring (Str. lastindexof (". ") + 1 ));
Example 3:
String Path = "http: /// localhost: 8080/client/index. JSP "; // after the last slash ,'. 'previous part (that is, 'index') is truncated to string x = path. substring (path. lastindexof ("/") + 1, path. lastindexof (". "); system. out. println (X );
String truncation examples