我将它们分开以获得第一个字母为大写现在我有问题合并它们并且第一个字母仍然是大写的。我的数据也来自数据库
private void button1_Click(object sender, EventArgs e)
{
//input = input.Replace("_", "");
string input;
input = table_menu.Text;
string[] words = input.Split('_');
foreach (string word in words)
{
string nword = word.First().ToString().ToUpper() + String.Join("", word.Skip(1));
string merge = String.Join("", nword);
MessageBox.Show(merge);
}
label1.Text = input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
Console.WriteLine(label1.Text);
}
当前输出:tablepatient
我想要一个像这样:
TablePatient
答案 0 :(得分:3)
资本化的概念是文化特定的 - 一种文化中的资本化可能与另一种文化中的资本化不同。如果要将字符串序列化为XML以进行持久存储,则可能需要使用invariant culture;如果您向用户展示它们,那么local culture(或者local UI culture)是合适的。
话虽这么说,the following可能会完成这项工作:
public static string UnderscoreToTitleCase(string input)
{
return UnderscoreToTitleCase(input, System.Globalization.CultureInfo.CurrentCulture);
}
public static string UnderscoreToTitleCaseInvariant(string input)
{
return UnderscoreToTitleCase(input, System.Globalization.CultureInfo.InvariantCulture);
}
public static string UnderscoreToTitleCase(string input, CultureInfo cultureInfo)
{
string[] words = input.Split('_');
StringBuilder sb = new StringBuilder();
foreach (string word in words)
sb.Append(cultureInfo.TextInfo.ToTitleCase(word));
return (sb.ToString());
}
答案 1 :(得分:1)
private void button1_Click(object sender, EventArgs e)
{
string input;
input = table_menu.Text;
string[] words = input.Split('_');
StringBuilder sb = new StringBuilder();
foreach (string word in words)
{
string nword = word.First().ToString().ToUpper() + String.Join("", word.Skip(1));
string merge = String.Join("", nword);
MessageBox.Show(merge);
sb.Append(nword);
}
label1.Text = sb.ToString();
Console.WriteLine(label1.Text);
}
答案 2 :(得分:0)
这有效:
var input = "table_patient";
var output = String.Join("",
input
.Split('_')
.Where(x => !String.IsNullOrEmpty(x))
.Select(x => new string(
x
.Take(1)
.Select(c => char.ToUpperInvariant(c))
.Concat(x.Skip(1))
.ToArray())));
//output == "TablePatient"
这也有效:
var output = System
.Globalization
.CultureInfo
.CurrentCulture
.TextInfo
.ToTitleCase(input)
.Replace("_", "");