locked
C# ToLower

    Question

  • Do we need to use culture info ToLower function as shown below or a normal ToLower() function; which one do we need to stick??

    string sample = "UPPERCASE STRING";

            // Get current culture
            CultureInfo culture = CultureInfo.CurrentCulture;

            // Call ToLower instance method with globalization parameter
            string lower = sample .ToLower(culture);

    Monday, May 24, 2010 7:56 AM

Answers

  • The actual implementation of string.TolLower() with no parameter is this:

    public string ToLower()
    {
        return this.ToLower(CultureInfo.CurrentCulture);
    }

    Therefore if you want to use the current culture, use the one without a parameter. Otherwise, you have no option other than to use the version where you pass in the culture to use.

    Personally, other than in test code, I only ever use the current culture or  CultureInfo.InvariantCulture when I want to persist or parse culture-sensitive data to or from files.

    Monday, May 24, 2010 8:46 AM

All replies

  • i think there is no need of cultureinfo you can simply convert it like this-

    String sample = "UPPERCASE STRING";

    sample = sample.ToLower();

     

    regards

    jayant

    Monday, May 24, 2010 8:05 AM
  • looks like "sample .ToLower(culture);" is fastest when compared to "sample.ToLower();"
    Monday, May 24, 2010 8:06 AM
  • Hi Sruthi,

    The casing rules of the culture that is currently in effect determines the way the case of a string is changed. If you do not specify the culture, the ToLower() method will convert the string to lower case based on the current culture settings. If the culture of the application and the local culture settings periodically changing, you will get different results for the string used with this method. So if you hard coded the result returned by the ToLower() method in your code(sometimes in if  condition), you may get unexpected results. So depending on your application requirement and the customer base, you need to use culture parameter in the ToLower() method. If you specify a culture settings at application level, you may  not need to specify the culture parameter.

    Monday, May 24, 2010 8:19 AM
  • Its better to use culture if your application spans across multiple cultures. Particularly when converting numeric values to other cultures its better to use that.

    Thanks,

    Thani


    Hei
    Monday, May 24, 2010 8:24 AM
  • The actual implementation of string.TolLower() with no parameter is this:

    public string ToLower()
    {
        return this.ToLower(CultureInfo.CurrentCulture);
    }

    Therefore if you want to use the current culture, use the one without a parameter. Otherwise, you have no option other than to use the version where you pass in the culture to use.

    Personally, other than in test code, I only ever use the current culture or  CultureInfo.InvariantCulture when I want to persist or parse culture-sensitive data to or from files.

    Monday, May 24, 2010 8:46 AM
  • Matthew nailed it, you should use the CultureInfo.CurrentCulture
    My .NET Blog: http://michaelcrump.net
    Monday, May 24, 2010 4:49 PM
  • The actual implementation of string.TolLower() with no parameter is this:

    public string ToLower()
    {
        return this.ToLower(CultureInfo.CurrentCulture);
    }

    Therefore if you want to use the current culture, use the one without a parameter. Otherwise, you have no option other than to use the version where you pass in the culture to use.

    Personally, other than in test code, I only ever use the current culture or  CultureInfo.InvariantCulture when I want to persist or parse culture-sensitive data to or from files.


    Wouldn't it be like this Matthew?

    public static class StringConversions
        {
            public static String ToMyLower(this string s)
            {
                return s.ToLower(CultureInfo.CurrentCulture);
            }       
        }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Globalization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          String sample = "UPPERCASE WORD";
          Console.WriteLine(sample.ToMyLower());
          Console.ReadLine();      
        }    
      }
    
      public static class StringConversions
      {
        /// <summary>
        /// Converts to current culture of ToLower
        /// </summary>
        /// <param name="s"></param>
        /// <returns>String</returns>
        public static String ToMyLower(this String s)
        {
          return s.ToLower(CultureInfo.CurrentCulture);
        }
      }
    
      
    }
    


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, May 24, 2010 8:37 PM
  • Wouldn't it be like this Matthew?

    <snip>

    I just pasted the code directly from Reflector, so I'm pretty sure the actual implementation of String.ToLower() is exactly what I posted. (I wasn't posting sample code for a user program.)
    Monday, May 24, 2010 11:25 PM
  • Wouldn't it be like this Matthew?

    <snip>

    I just pasted the code directly from Reflector, so I'm pretty sure the actual implementation of String.ToLower() is exactly what I posted. (I wasn't posting sample code for a user program.)
    Understood..

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, May 25, 2010 12:16 AM