Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the faster way of trying to find a single character on a String?

Tags:

string

c#

.net

What of this code is faster/more efficient? :

Boolean contains = myString.IndexOf("~", StringComparision.InvariantCultureIgnoreCase)!=-1;

or

Boolean contains = myString.IndexOf('~')!=-1;

I think the second because is a single character, but using the invariant culture ignore case comparer is supposed to be fast too :P

Cheers.

like image 486
vtortola Avatar asked Nov 26 '25 21:11

vtortola


1 Answers

The invariant culture is faster than other cultures, but ordinal comparison is even faster. Making it case insensetive is slower for these settings. So the fastest string comparison setting is StringComparison.Ordinal.

Searching for a character is about twice as fast as the fastest string search.

like image 143
Guffa Avatar answered Nov 28 '25 10:11

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!