简体   繁体   English

检查字符串在 c# 中是否有大写字母的最快方法是什么?

[英]what is the fastest way to check whether string has uppercase letter in c#?

My first implementation idea is to do simply:我的第一个实现想法是简单地做:

bool hasUpperCase (string str) {
    if(string.IsNullOrEmpty(str))
         return false;
    for (int i = 0; i < str.Length; i++) {
        if (char.IsUpper (str[i]))
            return true;                    
    }
    return false;
}

but maybe there is another faster way to do that?但也许还有另一种更快的方法可以做到这一点?

You could reduce that to你可以减少到

bool HasUpperCase (string str) {
    return !string.IsNullOrEmpty(str) && str.Any(c => char.IsUpper(c));
}

using LINQ.使用 LINQ。

Cheating from here: 从这里作弊:

bool hasUpperCase (string str) {
 if(string.IsNullOrEmpty(str))
     return false;

  return str != str.ToLower();
}

ok - time for the new truth!好的——是时候了解新的真相了!

This was a test for any upper case character in a string.这是对字符串中任何大写字符的测试。

The string was guaranteed to not have any upper case character within the first 60K of characters.保证字符串在前 60K 字符中没有任何大写字符。 (I created the string from random.org) (我从 random.org 创建了字符串)

I prevented string substitution optimization in the compiler by randomizing which 64K character string was passed to the test function.我通过随机化将哪个 64K 字符串传递给测试 function 来阻止编译器中的字符串替换优化。

All timings were very strictly around the actual test, and did not include function calling time.所有时序都非常严格地围绕实际测试,不包括 function 调用时间。

I ran the test once, 10 times, and again for 10,000 times and averaged each set of the timings for each test.我运行了一次、10 次和 10,000 次测试,并对每次测试的每组时间进行平均。

I ran the test on a 64bit Win 7 with i3-2100 CPU @ 3.1 Ghz我在具有 i3-2100 CPU @ 3.1 Ghz 的 64 位 Win 7 上运行了测试

Test Case 1:测试用例 1:

   static bool testCaseOne(string str, out double ms)
    {
        bool result = false;
        DateTime start = DateTime.Now;

        result = !string.IsNullOrEmpty(str) && str.Any(c => char.IsUpper(c));
        ms = (DateTime.Now - start).TotalMilliseconds;
        return result;
    }

Resulting average time:结果平均时间:

  1. 1 X = 3.000 ms 1 X = 3.000 毫秒
  2. 10 x = 0.860 ms 10 x = 0.860 毫秒
  3. 10,000 x = 0.821 ms 10,000 x = 0.821 毫秒

Test Case 2:测试用例 2:

    static bool testCaseTwo(string str, out double ms)
    {
        bool result = false;
        DateTime start = DateTime.Now;

        if (string.IsNullOrEmpty(str))
        {
            ms = 0;
            return false;
        }
        result = Regex.IsMatch(str, "[A-Z]");

        ms = (DateTime.Now - start).TotalMilliseconds;

        return result;
    }

Resulting average time:结果平均时间:

  1. 1 x = 2.000 ms 1 x = 2.000 毫秒
  2. 10 x = 1.597 ms 10 x = 1.597 毫秒
  3. 10,000 x = 1.603 ms 10,000 x = 1.603 毫秒

Test Case 3:测试用例 3:

   static bool testCaseThree(string str, out double ms)
    {
        bool result = false;
        DateTime start = DateTime.Now;

        if (string.IsNullOrEmpty(str))
        {
            ms = 0;
            return false;
        }
        for (int i = 0; i < str.Length; i++)
        {
            if (char.IsUpper(str[i]))
            {
                result = true;
                break;
            }
        }
        ms = (DateTime.Now - start).TotalMilliseconds;
        return result;
    }

Resulting average time:结果平均时间:

  1. 1 x = 1.000 ms 1 x = 1.000 毫秒
  2. 10 x = 0.357 ms 10 x = 0.357 毫秒
  3. 10,000 x = 0.298 ms 10,000 x = 0.298 毫秒

Test Case 4:测试用例 4:

    static bool testCaseFour(string str, out double ms)
    {
        bool result = false;
        DateTime start = DateTime.Now;

        if (string.IsNullOrEmpty(str))
        {
            ms = 0;
            return false;
        }
        for (int i = 0; i < str.Length; i++)
        {

            if (str[i] > 64 && str[i] < 91)
            {
                result = true;
                break;
            }
        }
        ms = (DateTime.Now - start).TotalMilliseconds;
        return result;
    }

}

Resulting average time:结果平均时间:

  1. 1 x = 0.000 ms 1 x = 0.000 毫秒
  2. 10 x = 0.137 ms 10 x = 0.137 毫秒
  3. 10,000 x = 0.184 ms 10,000 x = 0.184 毫秒

Interesting.有趣的。

I hope this statisfies Mr. R.我希望这能满足 R 先生的要求。 K. ;) K. ;)

bool hasUpperCase(string str) {
    if (string.IsNullOrEmpty(str))
        return false;
    return Regex.IsMatch(str, "[A-Z]");
}

Disclaimer : I am no Regex expert but I tested this with the strings Testing, testinG, and tesTing, which all evaluated to true.免责声明:我不是正则表达式专家,但我使用字符串Testing, testinG, and tesTing,对此进行了测试,这些字符串都被评估为 true。 However, it also evaluated to true with the string TESTING , which you may or may not want.但是,它也使用字符串TESTING评估为 true,您可能想要也可能不想要。

The code looks fine to me, since you ask for performance, you can reduce the for loop from O(n) to O(n/2 + ~1) by adding the conditional checking from the reverse side.代码对我来说看起来不错,因为您要求性能,您可以通过从反面添加条件检查将 for 循环从 O(n) 减少到 O(n/2 + ~1)。

Otherwise you can check two sub-sequent elements and increment the i by 2. Obviously you should check for i < str.Length for the second argument.否则,您可以检查两个后续元素并将 i 增加 2。显然,您应该检查第二个参数的 i < str.Length。

bool hasUpperCase (string str) {
if(string.IsNullOrEmpty(str))
     return false;
for (int i = 0; i < str.Length; i= i + 2) {
    if (char.IsUpper (str[i]))
        return true;                    

    if ((i + 1) < str.Length && char.IsUpper (str[i+1]))
        return true;                    
}
return false;

} }

IMHO, this tip may help to answer algorithm interview, doesn't get much performance.恕我直言,这个技巧可能有助于回答算法面试,并没有得到太多的表现。

    public static string Upper_To_Lower(string text)
    {
        if (Char.IsUpper(text[0]) == true) { text = text.Replace(text[0], char.ToLower(text[0])); return text; }

        return text;
    }

    public static string Lower_To_Upper(string text)
    {
        if (Char.IsLower(text[0]) == true) { text = text.Replace(text[0], char.ToUpper(text[0])); return text; }

        return text;
    }

Here i made 2 simple methods who check the first letter of any string and convert it from Upper to Lower and virse verca.... hope that will help you.在这里,我做了 2 个简单的方法来检查任何字符串的第一个字母并将其从 Upper 转换为 Lower 和 virse verca.... 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM