Java在方法中使用標簽
如何使用方法重載用於打印不同類型的數組?
解決方法
此示例演示如何跳轉到一個特定的標簽,當break或continue語句出現在一個循環中。
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("Found the substring ."); } else { System.out.println("did not find the substing in the string."); } } }
結果
上麵的代碼示例將產生以下結果。
Found the substring