鉴于这些情况:
string s1 = "Abc";
string s2 = "ABC";
更快:
Regex.Match(s1, s2, RegexOptions.Ign或eCase)
或
s1.ToLower() == s2.ToLower()
If they are the same 或 the one is faster then the other, so when its better to use one over the other?
鉴于这些情况:
string s1 = "Abc";
string s2 = "ABC";
更快:
Regex.Match(s1, s2, RegexOptions.Ign或eCase)
或
s1.ToLower() == s2.ToLower()
If they are the same 或 the one is faster then the other, so when its better to use one over the other?
也许第二步比较快,但我避免这两种做法。
更好的方法是使用string.Equals
。 缩略语 论点:
s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase)
见网站:。
Theoretically speaking, comparing 2 strings should be faster, RegEx are know to be rather slow.
However, if you want to match a string s1 to a RegEx s2 while ignoring case (This is not the same as comparing 2 strings), then the first solution is better as it should avoid creating another string.
与这种问题总是一样,我将有一个基准,对两个业绩进行比较:
@Mark Byers已经张贴了正确的答案。
我想强调,您应为never使用Tolower进行细微比较。 这是不正确的。
s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase) //#1
s1.ToLower() == s2.ToLower() //#2
s1.ToLowerInvariant() == s2.ToLowerInvariant() //#3
(2) and (3) are both incorrect when it comes to exotic languages and strange characters. The Turkish "I" is the classical example.
www.un.org/spanish/ecosoc 航道使用第1号,甚至在哈希姆。
应当指出,<代码>Regex.Match(s1, s2, RegexOptions. IgnoreCase)不是在一般情况下检查对案件敏感的平等的一种安全方法。 审议s2
is *>的
.Regex.Match
将始终如一地交回s
!
This may be the most extreme case of premature optimization I ve ever seen. Trust me, you will never run into a situation where this issue will be relevant.
而不要听从所有那些告诉你们的人,不要因为“the慢”而避开reg。
这里对拟议的3种方法进行了小的比较:
Regex: 282ms ToLower: 67ms Equals: 34ms
public static void RunSnippet()
{
string s1 = "Abc";
string s2 = "ABC";
// Preload
compareUsingRegex(s1, s2);
compareUsingToLower(s1, s2);
compareUsingEquals(s1, s2);
// Regex
Stopwatch swRegex = Stopwatch.StartNew();
for (int i = 0; i < 300000; i++)
compareUsingRegex(s1, s2);
Console.WriteLine(string.Format("Regex: {0} ms", swRegex.ElapsedMilliseconds));
// ToLower
Stopwatch swToLower = Stopwatch.StartNew();
for (int i = 0; i < 300000; i++)
compareUsingToLower(s1, s2);
Console.WriteLine(string.Format("ToLower: {0} ms", swToLower.ElapsedMilliseconds));
// ToLower
Stopwatch swEquals = Stopwatch.StartNew();
for (int i = 0; i < 300000; i++)
compareUsingEquals(s1, s2);
Console.WriteLine(string.Format("Equals: {0} ms", swEquals.ElapsedMilliseconds));
}
private static bool compareUsingRegex(string s1, string s2)
{
return Regex.IsMatch(s1, s2, RegexOptions.IgnoreCase);
}
private static bool compareUsingToLower(string s1, string s2)
{
return s1.ToLower() == s2.ToLower();
}
private static bool compareUsingEquals(string s1, string s2)
{
return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
}
比较速度较快,但不能转换为低级或高级案件,然后比较,最好采用平等比较办法,使情况敏感。 例如:
s1.Equals(s2, StringComparison.OrdinalIgnoreCase)
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...