3
How to count how many uppercase characters are in a string?
3
How to count how many uppercase characters are in a string?
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++;
I did 4 performance tests:
Sm str - LINQ
: test with 13 chars string using LINQSm str - FOR
: test with 13 chars string using for
Lg str - LINQ
: 13*1024 chars string test using LINQLg 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)
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
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 c#
You are not signed in. Login or sign up in order to post.
I learned one more. LINQ just doesn’t make it rain!
– rubStackOverflow
It’s really cool... but not always efficient. So I gave two options: readable and fast.
– Miguel Angelo
A good example where LINQ is more readable!
– ramaral
I edited and includes performance tests.
– Miguel Angelo
I believe that in most applications these differences are not relevant.
– ramaral
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.– Miguel Angelo
Got a tip on how/where to learn/improve knowledge about LINQ? @Miguelangelo
– rubStackOverflow
@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
andIQueryable
, classesEnumerableExtensions
andQueryableExtensions
, 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.– Miguel Angelo