java query string from one location to another string

Method a: Use regionMatches () method;

                string is always a string, getString a string to query.

                 regionMatches () is true (case-insensitive, to distinguish between to false), I (traverse to the position of the total number of strings), 0 (getString start position), the last one is the length of getString.

. 1  for ( int I = 0; I <String.length (); I ++ ) {
 2      IF (I <= String.length () - getString.length ()) {
 . 3          IF (string.regionMatches ( to true , I, the getString , 0 , getString.length ())) {
 . 4              System.out.println (I); // I is the starting position of the target string, the end position is the getString.length + I (); . 5          }
 . 6      }
 . 7  }          

Method two: Use the indexOf () method;

    If the process target string is found, then the starting position of the target string is returned (greater than 0); if not, returns -1.

. 1  for ( int I = 0; I <String.length (); I ++ ) {
 2      IF (I <= String.length () - getString.length ()) {
 . 3          IF (String.IndexOf (the getString, I)> 0 ) {
 . 4              I = String.IndexOf (the getString, I);
 . 5              System.out.println (I); // I is the starting position of the target string, the end position is the getString.length + I (); 
. 6          }
 7      }
 8 }

 

Guess you like

Origin www.cnblogs.com/mengweihong/p/11305056.html