C#:比较二个字符串,查找出相同字数和差异字符

时间:2022-06-14 00:41:27
        /// <summary>
        /// 比较二个字符串,查找出相同字数和差异字符
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        public int sCompare(string s1, string s2)
        {
            int count = ;/*相同字符个数*/
            int n = s1.Length > s2.Length ? s2.Length : s1.Length;/*获得较短的字符串的长度*/
            for (int i = ; i < n; i++)
            {
                if (s1.Substring(i, ) == s2.Substring(i, ))
                /*同位置字符是否相同*/
                {
                    count++;
                }
                else
                {
                    //MessageBox.Show("s1:" + s1.Substring(i, 1) + "| s2:" + s2.Substring(i, 1));
                }
            }
            return count;
        }