找出2个字符串中所有相同的字符

没有去重,需要加入到 Set 集合中,在打印

	static String a = "afafa";
	static String b = "asdf";

public static void main(String[] args) {
		test1();
		//test2();
	}

public static void test1() {
		final int aSize = a.length();
		for (int i = 0; i < aSize; i++) {
			for (int j = aSize; j != i; j--) {
				String s1 = a.substring(i, j);
				if (b.contains(s1)) {
					System.out.println(s1);
				}
			}
		}
	}


public static void test2() {
		final int aSize = a.length();
		for (int i = 0, j = aSize; i < aSize; i++) {
			String s1 = a.substring(i, j);
			if (b.contains(s1)) {
				System.out.println(s1);
			}
			j--;
			if (j != i) {
				i--;
			} else {
				j = aSize;
			}
		}
	}

你可能感兴趣的:(Java)