c# - 如何像 windows 那样比较文本?
问题描述
可能人们知道,string.Compare 将文本与数字进行比较为“xx10yy”<“xx2yy”,但是,Windows 将其作为“xx10yy”>“xx2yy”。
问题是如何像 windows 那样比较文本,如果有一个函数可以做到这一点。
提前致谢。
编辑 :
这个问题是在很多问题中以多种方式写的,我认为我的更严格,因为我提供的答案是我写的,所以我更喜欢回答我喜欢的问题。
编辑 2:
只是为了不将其标记为重复,因为它不是,至少不是我所知道的。
解决方案
这就是我所做的方式,我相信它会帮助一些人,请告诉我是否有人发现其中的错误。
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;
}
}
推荐阅读
- java - 商品数量折扣
- android - 如何删除我的布局背景以便我可以看到它背后的活动?
- python - 从 QFrame 内的 QPushButton 到另一个 QWidget 的单击事件
- c# - C#如何验证字符串属性只接受10位数字
- java - Pdf文件重命名和删除在我的Android应用程序中不起作用
- python - 如何为播放器和计算机添加指点系统。先得3分者获胜
- javascript - 从 node_modules 向 vue 发送事件
- javascript - 我想更改 API 响应 JSON 格式
- php - 制作 CMS 网站不加载 CSS 和 JavaScript 文件
- laravel - 根据 Laravel 8 将 laravel db 模型移动到文件夹 Models (App\Models) 中会出现错误