27

We have noticed a weird error when calling ToLower() on certain strings.

The input string is:

string s = "DocumentInfo";
string t = s.ToLower();
// sometimes, t == documentinfo
// other times, t == documentınfo  (note dot is missing from i - INCORRECT)

We are passing the string to a web service query downstream, so it is causing problems for us.

My initial guess is that it has something to do with Culture or UICulture, as some of our pages customize these settings per user.

Could this be the issue? Is there are way I can force this to work properly?

UPDATE 2011.07.06

I found that I could duplicate the issue by setting Culture to tr-TR. Not sure if other cultures are impacted.

frankadelic
  • 20,543
  • 37
  • 111
  • 164

2 Answers2

33

Try using String.ToLowerInvariant().

Matthew
  • 28,056
  • 26
  • 104
  • 170
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
3

Try :

s.ToLower(new CultureInfo("en-US", false));

If you get a different result, your CultureInfo.CurrentCulture may be set to something else.

Eric H
  • 1,759
  • 1
  • 11
  • 14
  • 8
    While this will probably work fine, the invariant culture is more convenient (you don't need to look it up by key), and more indicative of our intent, IMO – Marc Gravell Jul 06 '11 at 18:18
  • The user should just know that the Invariant in ToLowerInvariant assumes they want lower case general English. Per MSDN: It is associated with the English language but not with a country or region. – Eric H Jul 06 '11 at 18:27
  • 2
    But using the invariant culture is not the same thing as using the en-US culture. If you see ToLowerInvariant, you can assume it's going to be a non-visible string used for internal computation. If you see "en-US", you have no idea what's going on. Also, the first is so much easier to write. – R. Martinho Fernandes Jul 06 '11 at 18:30
  • 1
    CultureInfo seems undefined? – Bengi Besçeli Nov 16 '15 at 13:27