java字符串比较题,对比字符串中,对应位置字母一样的个数,从前往后比对。

java字符串比较题,对比字符串中,对应位置字母一样的个数,从前往后比对。比如输入了

String str1 = "a,b,c";

String str2 = "a,b,d";

有a,b是对应相同的,就输出2


package 字符串;

public class DifferentStringNum {
	public static void main(String[] args) {
		String str1 = "a,b,c";
		String str2 = "a,b,d";
		System.out.println(DifferentStringNum.findDifferentString(str1, str2));

	}

	// TODO Auto-generated method stub
	public static int findDifferentString(String str1, String str2) {
		String[] str3 = str1.split(",");
		String[] str4 = str2.split(",");
		int len1 = str3.length;
		int len2 = str4.length;
		int len = len1 > len2 ? len2 : len1;
		int n = 0;
		for (int i = 0; i < len; i++) {
			if (str3[i].equals(str4[i])) {
				n++;
			}
		}

		return n;

	}

}

控制台打印:


猜你喜欢

转载自blog.csdn.net/handsome2013/article/details/80442101