转换字母的大小写
eg1:
private void buttonX1_Click(object sender, EventArgs e)
{
string test = "afsfsdf,sdfsfdsf,sdfdsfsdf,AFSFASF,adfsf,afdsf,,,,,";
string firstUpper = toUpper(test);
string uppers = firstUpper.Replace(",","");
Console.WriteLine(test);//原始数据
Console.WriteLine(uppers);//将首字母转换成大写,去除逗号
Console.WriteLine(uppers.ToLower());//将字母全部转换为小写,去除逗号
string result = test.Replace(",", " ");//将逗号转换为空格
Console.WriteLine(result.Trim ().Replace(" ","_").ToLower()); //清空空格,将空格转换为下滑线
}
//将首字母转换成大写
public static string toUpper(string text)
{
bool b = true;//逗号的下一位
int charNum = 0;//第一位
char[] strChar = text.ToCharArray();
for (int i = 0; i < strChar.Length; i++)
{
charNum = (int)strChar[i];
if (b && charNum > 96 && charNum < 123)
{
strChar[i] = (char)(charNum - 32);//代表大小写相差32
b = false;
continue;
}
if (charNum == 44)//判断是否为逗号
{
b = true;
continue;
}
b = false;
}
return new string(strChar);
}
eg2:
public static class TextTo
{
/// <summary>
/// 单词首字母大写
/// </summary>
/// <param name="text">需要转换的文本</param>
/// <param name="str">判定字符</param>
/// <param name="isFirst">是否验证第一个字母</param>
/// <returns>转换后的文本</returns>
public static string ToUpper(string text, char str, bool isFirst)
{
bool b = isFirst;//设定字符的下一位
int charNum = 0;//第一位
char[] strChar = text.ToCharArray();
for (int i = 0; i < strChar.Length; i++)
{
charNum = (int)strChar[i];
if (b && charNum > 96 && charNum < 123)
{
strChar[i] = (char)(charNum - 32);//代表大小写相差32
b = false;
continue;
}
if (strChar[i] == str)//判断是否为设定字符
{
b = true;
continue;
}
b = false;
}
return new string(strChar);
}
}
public class WordText
{
public WordText(string text)
{
if ((text = text.Trim().ToLower()).Length == 0)
{
return;
}
AllUp = TextTo.ToUpper(text, ',', true).Replace(",", "");
FirstNoUp = AllUp.Substring(0, 1).ToLower() + AllUp.Substring(1);
UnderlineText = text.Replace(',', '_');
}
public string AllUp
{
get;
set;
}
public string FirstNoUp
{
get;
set;
}
public string UnderlineText
{
get;
set;
}
}