Latest web development tutorials

Java Examples - Label (Label)

Java Examples Java Examples

The Java tag is designed for the cycle, in order to facilitate the use of multiple loop break and coutinue.

The following example jumps to the specified label when used at the break or continue circulating in the loop:

/*
 author by w3cschool.cc
 Main.java
 */

public class Main {
   public static void main(String[] args) {
      String strSearch = "This is the string in which you have to search for a substring.";
      String substring = "substring";
      boolean found = false;
      int max = strSearch.length() - substring.length();
      testlbl:
      for (int i = 0; i <= max; i++) {
         int length = substring.length();
         int j = i;
         int k = 0;
         while (length-- != 0) {
            if(strSearch.charAt(j++) != substring.charAt(k++)){
               continue testlbl;
            }
         }
         found = true;
         break testlbl;
      }
      if (found) {
         System.out.println("发现子字符串。");
      }
      else {
         System.out.println("字符串中没有发现子字符串。");
      }
   }
}

The above code is run output is:

发现子字符串。

Java Examples Java Examples