5

I have read that when in your application you do a lot of string comparison and using ToLower method, this method is quite costly. I was wondering of anyone could explain to me how is it costly. Would appreciate any info or explanation. Thanks!

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
Coding Duchess
  • 6,445
  • 20
  • 113
  • 209
  • Unless these are "significantly large" strings or they are of "innumerable quantity", this likely won't be a bottleneck .. ever. That being said, such normalization process - and especially in the case of in-memory strings - is a round-about way to describe the desired task. But such normalization is not always "bad", especially if the results are used [otherwise] anyway. – user2864740 Feb 10 '15 at 20:14

5 Answers5

11

See also writing culture-safe managed code for a very good reason why not to use ToLower().

In particular, see the section on the Turkish "I" - it's caused no end of problems in the past where I work...

Calling "I".ToLower() won't return "i" if the current culture is Turkish or Azerbaijani. Doing a direct comparison on that will cause problems.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
10

There is another advantage to using the String.Compare(String, String, StringComparison) method, besides those mentioned in the other answers:

You can pass null values and still get a relative comparison value. That makes it a whole lot easier to write your string comparisons.

String.Compare(null, "some StrinG", StringComparison.InvariantCultureIgnoreCase);

From the documentation:

One or both comparands can be null. By definition, any string, including the empty string (""), compares greater than a null reference; and two null references compare equal to each other.

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
  • can I do the same with String.StartsWith()? – Coding Duchess Feb 10 '15 at 20:46
  • @ElenaDBA No. According to the documentation `string.StartsWith()` will throw for a `null` argument. See https://msdn.microsoft.com/en-us/library/baketfxw%28v=vs.110%29.aspx – oɔɯǝɹ Feb 10 '15 at 20:48
  • Actually what I mean iif I use StringComparison.InvariantCultureIgnoreCase as an argument String.StartsWith(), will it improve the performance? – Coding Duchess Feb 11 '15 at 14:30
  • @ElenaDBA what do you mean? would you like to call `StartsWith()` first before calling `Compare()`? string `Compare()` is a 'smart' method, it will stop comparing after the first mismatch. -- I wouldn't be so concerned about performance, unless you have an actual performance problem. And when you do, use a profiler to find the actual bottleneck. I think you will find it is not were you will think it is. – oɔɯǝɹ Feb 11 '15 at 20:26
8

It's costly because a new string is "manufactured".

Compare that to calling, say, Equals with an overload that asks for a case-insensitive comparison. This allows the comparison to terminate, without having to create a new string, as soon as a mismatch is identified.

user2864740
  • 60,010
  • 15
  • 145
  • 220
3

Each time you call ToLower(), a new copy of the string will be created (as opposed to making the case changes in-place). This can be costly if you have many strings or long strings.

From String.ToLower docs:

Returns a copy of this string converted to lowercase.

ama1111
  • 569
  • 3
  • 10
2

As somebody already answered, ToLower() will create a new string object, which is extra cost comparing to using "IgonoreCase". If this ToLower is triggered frequently, you end up creating a lot of small objects in your heap, and will add Garbage Collection time, which becomes a performance penalty.