使用內置方法比較c#中的2個字符串不同的情況[​​重復]

[英]compare a 2 string in c# using inbuilt method different case [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

How to compare a 2 string in different case like String a="Pawan"; String b="PAWAN"; how to compare using inbuilt method I am try to compare culture method but can not be compere so please provide a solution return result equal or not equal

如何比較不同情況下的2個字符串,如String a =“Pawan”;字符串b =“PAWAN”;如何比較使用內置方法我嘗試比較文化方法,但不能主持,所以請提供一個解決方案返回結果相等或不相等

2 个解决方案

#1


-1  

Transform both to lowercase strings and compare after that

將兩者轉換為小寫字符串並在此之后進行比較

a.ToLower() == b.ToLower()

#2


-1  

Every answer so far was checking for equality, here is mine actually comparing:

到目前為止,每個答案都在檢查是否平等,這是我的實際比較:

switch(string.Compare(a, b, StringComparison.CurrentCultureIgnoreCase))
{
   case  1: Console.WriteLine("a is greater"); break;
   case  0: Console.WriteLine("a and b are equal"); break;
   case -1: Console.WriteLine("b is greater"); break;
}

You can test it here https://dotnetfiddle.net/h75xZ8

你可以在這里測試它https://dotnetfiddle.net/h75xZ8

If you arent sure about the comparison, you can look it up here: https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx

如果你不確定比較,你可以在這里查找:https://msdn.microsoft.com/en-us/library/system.stringcomparison(v = vs.110).aspx


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/08/09/720d3a101ce9ef2dfe0b5f4fffb03e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com