String searchMe = "Look for a substring in me";
String substring = "sub";
test: // 標籤
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i, k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test; // 跳轉到 test 標籤的下一次迭代 (i++)
}
}
foundIt = true;
break test; // 完全匹配,跳出外層循環
}
for 遍歷主串每個可能起始位置 i。while 逐字符比較子串。continue test 直接進入外層循環下一輪 (i++),跳過當前位置剩餘比較。foundIt=true; break test 跳出雙層循環。Java break/continue label 可控制多層循環,避免標誌變量,提高可讀性。