using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace 字符串首字母大写
{
static class Program
{
public static string FirstCharToUpper(string input)
{//两个字符串
if (String.IsNullOrEmpty(input))
throw new ArgumentException("ARGH!");
return input.First().ToString().ToUpper() + input.Substring(1);
}
public static string FirstLetterToUpper(string str)
{//两个字符串
if (str == null)
return null;
if (str.Length > 1)
return char.ToUpper(str[0]) + str.Substring(1);
return str.ToUpper();
}
public static unsafe string ToUpperFirst(this string str)
{//允许不安全代码
if (str == null) return null;
string ret = string.Copy(str);
fixed (char* ptr = ret)
*ptr = char.ToUpper(*ptr);
return ret;
}
static void Main(string[] args)
{
string s1 = "as";
string s11 = FirstCharToUpper(s1);
string s2 = "sd";
string s22 = FirstLetterToUpper(s2);
//只需一个 比较好方法
string s3 = "a";
string s33 = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s3.ToLower());
//只需一个
string s44 = new CultureInfo("en-US").TextInfo.ToTitleCase("r");
//需要两个 拼接
string s5 = "gs";
string s55 = s5.Remove(1).ToUpper() + s5.Substring(1);
//只需一个 性能比较好
string s6 = "g";
char[] s66 = s6.ToCharArray();
s66[0] = char.ToUpper(s66[0]);
string s666 = new string(s66);
//只需一个
string s7 = "r";
string s77 = System.Text.RegularExpressions.Regex.Replace(s7, "^[a-z]", m => m.Value.ToUpper());
string s8 = "p";
string s88 = Regex.Replace(s8, @"^\w", t => t.Value.ToUpper());
//选中“允许不安全代码”复选框。
//最好的速度
string s9 = "l";
string s99 = s9.ToUpperFirst();
}
}
}