C# 如何像windows那样比较文本?
人们可能知道,string.Compare将文本与数字比较为“xx10yy”<“xx2yy”,然而,windows将文本与数字比较为“xx10yy”>“xx2yy” 问题是,如果有一个函数可以做到这一点,那么如何像windows那样比较文本 提前谢谢 编辑: 这个问题在很多问题中都是用很多方式写的,我的观点更严格,因为我提供的答案是我自己写的,所以我更喜欢回答我喜欢的问题 编辑2:C# 如何像windows那样比较文本?,c#,text,C#,Text,人们可能知道,string.Compare将文本与数字比较为“xx10yy”“xx2yy” 问题是,如果有一个函数可以做到这一点,那么如何像windows那样比较文本 提前谢谢 编辑: 这个问题在很多问题中都是用很多方式写的,我的观点更严格,因为我提供的答案是我自己写的,所以我更喜欢回答我喜欢的问题 编辑2: 只是为了不被标记为重复,因为它不是,至少不是我所知道的一个。我就是这样做的,我相信它会对一些人有所帮助,请告诉我是否有人发现其中的错误 public class HumanizeCompa
只是为了不被标记为重复,因为它不是,至少不是我所知道的一个。我就是这样做的,我相信它会对一些人有所帮助,请告诉我是否有人发现其中的错误
public class HumanizeComparisonType
{
public class GetHumanizeComparisonType
{
public bool CaseSensitiveCondition;// True - case sensitive, False - case insensitive
public string
FirstText
, SecondText;
}
public static int HumanizeComparison(GetHumanizeComparisonType GetHumanizeComparison)// 0 - Equal, 1 - The first text is bigger, 2 - The second text is bigger
{
GetHumanizeComparisonType FunctionGet = GetHumanizeComparison;
int
FunctionResult = 0
, FirstTextIndex = -1
, FirstTextNumber
, FirstTextLength = FunctionGet.FirstText.Length
, SecondTextIndex = -1
, SecondTextNumber
, SecondTextLength = FunctionGet.SecondText.Length
, SmallestTextLength = FirstTextLength > SecondTextLength ? SecondTextLength : FirstTextLength;
bool MethodFound = false;
for (int TextsIndex = 0; TextsIndex < SmallestTextLength; TextsIndex++)
if
(
FunctionGet.FirstText[TextsIndex] >= '0' && FunctionGet.FirstText[TextsIndex] <= '9'
&& FunctionGet.SecondText[TextsIndex] >= '0' && FunctionGet.SecondText[TextsIndex] <= '9'
)
{
switch (String.Compare(FunctionGet.FirstText, FirstTextIndex + 1, FunctionGet.SecondText, FirstTextIndex + 1, TextsIndex - (FirstTextIndex + 1), FunctionGet.CaseSensitiveCondition == false))
{
case -1:
{
MethodFound = true;
FunctionResult = 2;
break;
}
case 0:
{
for (FirstTextIndex = TextsIndex; FirstTextIndex < FirstTextLength - 1; FirstTextIndex++)
if (FunctionGet.FirstText[FirstTextIndex + 1] < '0' || FunctionGet.FirstText[FirstTextIndex + 1] > '9')
break;
for (SecondTextIndex = TextsIndex; SecondTextIndex < SecondTextLength - 1; SecondTextIndex++)
if (FunctionGet.SecondText[SecondTextIndex + 1] < '0' || FunctionGet.SecondText[SecondTextIndex + 1] > '9')
break;
FirstTextNumber = int.Parse(FunctionGet.FirstText.Substring(TextsIndex, FirstTextIndex - TextsIndex + 1));
SecondTextNumber = int.Parse(FunctionGet.SecondText.Substring(TextsIndex, SecondTextIndex - TextsIndex + 1));
if (FirstTextNumber > SecondTextNumber)
{
MethodFound = true;
FunctionResult = 1;
}
else
{
if (SecondTextNumber > FirstTextNumber)
{
MethodFound = true;
FunctionResult = 2;
}
else
{
if (FirstTextIndex > SecondTextIndex)// checking if there're leading zeroes before the number of the first text
{
MethodFound = true;
FunctionResult = 2;
}
else
{
if (SecondTextIndex > FirstTextIndex)// checking if there're leading zeroes before the number of the second text
{
MethodFound = true;
FunctionResult = 1;
}
else
TextsIndex = FirstTextIndex;
}
}
}
break;
}
case 1:
{
MethodFound = true;
FunctionResult = 1;
break;
}
}
if (MethodFound == true)
break;
}
if (MethodFound == false)
if (FirstTextIndex < FirstTextLength - 1)
if (SecondTextIndex < SecondTextLength - 1)
switch (String.Compare(FunctionGet.FirstText.Substring(FirstTextIndex + 1), FunctionGet.SecondText.Substring(FirstTextIndex + 1), FunctionGet.CaseSensitiveCondition == false))
{
case -1:
{
FunctionResult = 2;
break;
}
case 1:
{
FunctionResult = 1;
break;
}
}
else
FunctionResult = 1;
else
{
if (SecondTextIndex < SecondTextLength - 1)// else means that both of the texts are equal, FunctionResult = 0
FunctionResult = 2;
}
return FunctionResult;
}
}
公共类人性化比较类型
{
公共类GetHumaniceComparsonType
{
public bool CaseSensitiveCondition;//True-区分大小写,False-不区分大小写
公共字符串
第一文本
,第二文本;
}
public static int-humanicecomparison(gethumanicecomparisontype gethumanicecomparison)//0-相等,1-第一个文本较大,2-第二个文本较大
{
GetHumanizeComparisonType函数Get=GetHumanizeComparison;
int
FunctionResult=0
,FirstTextIndex=-1
,FirstTextNumber
,FirstTextLength=FunctionGet.FirstText.Length
,SecondTextIndex=-1
,第二个文本编号
,SecondTextLength=函数get.SecondText.Length
,SmallestTextLength=FirstTextLength>SecondTextLength?SecondTextLength:FirstTextLength;
bool MethodFound=false;
对于(int-TextsIndex=0;TextsIndex='0'和&FunctionGet.FirstText[TextsIndex]='0'和&FunctionGet.SecondText[TextsIndex]'9')
打破
对于(SecondTextIndex=TextsIndex;SecondTextIndex'9')
打破
FirstTextNumber=int.Parse(FunctionGet.FirstText.Substring(TextsIndex,FirstTextIndex-TextsIndex+1));
SecondTextNumber=int.Parse(FunctionGet.SecondText.Substring(TextsIndex,SecondTextIndex-TextsIndex+1));
如果(第一个文本编号>第二个文本编号)
{
MethodFound=true;
函数结果=1;
}
其他的
{
如果(SecondTextNumber>FirstTextNumber)
{
MethodFound=true;
函数结果=2;
}
其他的
{
if(FirstTextIndex>SecondTextIndex)//检查第一个文本的编号前是否有前导零
{
MethodFound=true;
函数结果=2;
}
其他的
{
if(SecondTextIndex>FirstTextIndex)//检查第二个文本的编号前是否有前导零
{
MethodFound=true;
函数结果=1;
}
其他的
TextsIndex=第一个textIndex;
}
}
}
打破
}
案例1:
{
MethodFound=true;
函数结果=1;
打破
}
}
if(MethodFound==true)
打破
}
if(MethodFound==false)
if(FirstTextIndex
Windows资源管理器采用第二种方法,您可以实现一种自然排序算法来实现同样的操作。@Welcomeflow这里对问题的解释不同,我认为我的问题更严格,我也提供了自己编写的答案,所以我更喜欢回答我喜欢的问题。如果问题是如何“像Windows那样比较文本”使用sam