复制代码 代码如下:
private static bool CompareStringByChar(string strA,string strB)
{
bool IsEqual = true;
char[] arrA = strA.ToCharArray();
char[] arrB = strB.ToCharArray();
foreach (char chara in arrA)
{
if (!strB.Contains(chara))
{
IsEqual = false;
}
}
foreach (char charb in arrB)
{
if (!strA.Contains(charb))
{
IsEqual = false;
}
}
return IsEqual;
}
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringCompare
{
class Program
{
static void Main(string[] args)
{
string a = "4555";
string b = "5544";
Console.WriteLine(CompareStringByChar(a,b));
Console.Read();
}
//比较两个字符串是否含有完全相同的字符
private static bool CompareStringByChar(string strA,string strB)
{
bool IsEqual = true;
char[] arrA = strA.ToCharArray();
char[] arrB = strB.ToCharArray();
foreach (char chara in arrA)
{
if (!strB.Contains(chara))
{
IsEqual = false;
}
else
{
if(GetSameCharCount(chara,arrA)!=GetSameCharCount(chara,arrB))
{
IsEqual = false;
}
}
}
foreach (char charb in arrB)
{
if (!strA.Contains(charb))
{
IsEqual = false;
}
else
{
if (GetSameCharCount(charb, arrA) != GetSameCharCount(charb, arrB))
{
IsEqual = false;
}
}
}
return IsEqual;
}
//获得某字符在字符串中的数量
private static int GetSameCharCount(char chara,char[] arrChar)
{
int count = 0;
foreach(char a in arrChar)
{
if(chara==a)
{
count++;
}
}
return count;
}
}
}
复制代码 代码如下:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace StringCompare
{
class Program
{
static void Main(string[] args)
{
string strA = "中华人民共和国";
string strB = "共和国人民中华";
List<char> listA = strA.ToList();
List<char> listB = strB.ToList();
for (int i = 0; i < listA.Count;i++ )
{
for (int j = 0; j < listB.Count;j++ )
{
if (listA[i].ToString() == listB[j].ToString())
{
listA.RemoveAt(i);
listB.RemoveAt(j);
i--;
j--;
break;
}
}
}
if (listA.Count == 0 && listB.Count == 0)
{
Console.WriteLine("相等");
}
else
{
Console.WriteLine("不相等");
}
Console.Read();
}
}
}
新闻热点
疑难解答
图片精选