How to count how many uppercase characters are in a string?

Asked

Viewed 1,925 times

3

How to count how many uppercase characters are in a string?

2 answers

5


There are several ways to do this:

  • Using LINQ:

    var str = "Miguel Angelo";
    var contagem = str.Count(char.IsUpper);
    

    Note: Import LINQ namespace: using System.Linq;

  • Without using LINQ:

    var str = "Miguel Angelo";
    var contagem = 0;
    for (int itChar = 0; itChar < str.Length; itChar++)
        if (char.IsUpper(str[itChar]))
            contagem++;
    

Performance

I did 4 performance tests:

  • Sm str - LINQ: test with 13 chars string using LINQ
  • Sm str - FOR: test with 13 chars string using for
  • Lg str - LINQ: 13*1024 chars string test using LINQ
  • Lg str - FOR: test with 13*1024 chars string using for

Results:

Exec #1
    Sm str - LINQ     1.092µs (84ns por char)
    Sm str - FOR      0.462µs (36ns por char)
    Lg str - LINQ   609.931µs (46ns por char)
    Lg str - FOR    425.092µs (32ns por char)
Exec #2
    Sm str - LINQ     1.013µs (78ns por char)
    Sm str - FOR      0.436µs (34ns por char)
    Lg str - LINQ   606.826µs (46ns por char)
    Lg str - FOR    433.585µs (33ns por char)
Exec #3
    Sm str - LINQ     1.017µs (78ns por char)
    Sm str - FOR      0.459µs (35ns por char)
    Lg str - LINQ   616.975µs (46ns por char)
    Lg str - FOR    443.754µs (33ns por char)
Exec #4
    Sm str - LINQ     1.086µs (84ns por char)
    Sm str - FOR      0.458µs (35ns por char)
    Lg str - LINQ   683.890µs (51ns por char)
    Lg str - FOR    441.979µs (33ns por char)

Completion

You can only notice the difference when the string is greater than 100 million characters. It would be a ~100MB file in utf-8.

So: Use the LINQ version in 99.99% of cases.

  • 1

    I learned one more. LINQ just doesn’t make it rain!

  • It’s really cool... but not always efficient. So I gave two options: readable and fast.

  • A good example where LINQ is more readable!

  • I edited and includes performance tests.

  • 2

    I believe that in most applications these differences are not relevant.

  • 1

    They are not relevant at all. Only for very large strings. As you can see with a string of 13KB, it only takes 0.6ms, to be remarkable it would have to be a string of 130MB, there would be 6s in LINQ and 4s in for... but it’s still derisory.

  • Got a tip on how/where to learn/improve knowledge about LINQ? @Miguelangelo

  • 1

    @Hstackoverflow I learned everything on the internet and making code. There is a lot of material out there, but I think most of it is in English. LINQ is actually a set of things that work together to support that syntax of English: extension methods, interfaces IEnumerable and IQueryable, classes EnumerableExtensions and QueryableExtensions, inference of generic parameters, lambda expressions and closures... should have some other subjects... research on these things that will understand more about how LINQ works.

Show 3 more comments

1

You can do this using regular expression.
Import the namespace System.Text.Regularexpressions;

 var str = "Paulo Costa";
 var count = Regex.Matches(str, "[A-Z]").Count;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.