C#中字符,字符串的大小写转换

来源:互联网 发布:晾霸 好太太 知乎 编辑:程序博客网 时间:2024/05/20 15:40

字符串

对字符串来说,"string".ToLower()和"string".ToUpper()可以基本满足需求,但是当需要将首字母大写的时候,这两个函数就有点不够用了.但还好,我们还有TextInfo类下的ToLittleCase方法.在使用TextInfo类时,必须指定区域性.要获得区域性,必须能够访问当前线程,从该线程中检索CurrentCulture属性.

using System;using System.Globalization;using System.Threading;namespace csConsole{    public class MyClass     {        public static void Main() {   string title="this is my converted string";          Console.WriteLine("String Class");          Console.WriteLine("------------");          //Convert string to uppercase.          Console.WriteLine(title.ToUpper());          //Convert string to lowercase.          Console.WriteLine(title.ToLower());          Console.WriteLine();          Console.WriteLine("TextInfo Class");          Console.WriteLine("--------------");          //Get the culture property of the thread.          CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;          //Create TextInfo object.          TextInfo textInfo = cultureInfo.TextInfo;                              //Convert to uppercase.          Console.WriteLine(textInfo.ToUpper(title));          //Convert to lowercase.          Console.WriteLine(textInfo.ToLower(title));          //Convert to title case.          Console.WriteLine(textInfo.ToTitleCase(title));}    }}


字符

对字符来说,最初将一个字符变为小写,我是这样做的  char itemLower = Convert.ToChar(item.ToString().ToLower())  ,很傻很天真的方法.后来,我才查到char有静态方法ToUpper,ToLowwer,所以可以这样做.

                char itemLower = char.ToLower(item);                char itemUpper = char.ToUpper(item);





0 0