Java课本练习——寻找子串

public class ContinueWithLabel {
	public static void main(String[] args) {
		String searchMe = "Look for a substring in me";
		String substring  = "sub";
		boolean found = false;
		int max = searchMe.length() - substring.length();
	test:
		for(int i=0;i<=max;++i) {
			int n = substring.length();
			int j=i;
			int k=0;
			while(n--!=0) {
				if(searchMe.charAt(j++)!=substring.charAt(k++)) {
					continue test;
				}
			}
			found = true;
			break test;
		}
		System.out.println(found?"Found it":"Didn't find it");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44009743/article/details/89093833