Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串转换,每个单词的第一个字符_C#_String - Fatal编程技术网

C# 字符串转换,每个单词的第一个字符

C# 字符串转换,每个单词的第一个字符,c#,string,C#,String,我想转换: HECHT, WILLIAM 到 在c#中 有什么优雅的方法吗 string name = "HECHT, WILLIAM"; string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower()); (注意它只适用于从低到高的顺序,因此开始使用小写)我对Marc的答案投了赞成票,但这也适用于: string s = Microsoft.VisualBasic.Strings.StrConv("HEC

我想转换:

HECHT, WILLIAM 

在c#中

有什么优雅的方法吗

string name = "HECHT, WILLIAM";
string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower());

(注意它只适用于从低到高的顺序,因此开始使用小写)

我对Marc的答案投了赞成票,但这也适用于:

string s = Microsoft.VisualBasic.Strings.StrConv("HECHT, WILLIAM", VbStrConv.ProperCase,0);

您需要添加适当的引用,但我确信它适用于所有较高的输入。

我只想给出一个答案,指出虽然这在理论上看起来很简单,但在实践中,正确地大写每个人的名字可能非常复杂:


总之,我需要考虑一下。

我对上面的代码有问题,所以我对它做了一些修改,它成功了。智利的问候。好论文

private void label8_Click(object sender, EventArgs e)
{
nombre1.Text= NOMPROPIO(nombre1.Text);
}

string NOMPROPIO(string s)
{
  if (String.IsNullOrEmpty(s))
     s = "";
  string phrase = "";
  string[] words = s.Split(' ');
  foreach (string word in words)
  {
    if (word.Length > 1)
    phrase += word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + " ";
    else
    phrase += word.ToUpper() + " ";
  }
return phrase.Trim();
}

那东西在里面吗?哦,天哪+1@Marc:是否正确处理“Peter O'Toole”和“Mary Jones Smith”?@Grant:Peter需要一个新名字,Mary很好。@TruthStands:没有为“Peter O'Toole”和“Mary Smith Jones”生成正确的结果。是的,但解决这个问题并不困难。。对于西班牙人来说,有。
string s = Microsoft.VisualBasic.Strings.StrConv("HECHT, WILLIAM", VbStrConv.ProperCase,0);
private void label8_Click(object sender, EventArgs e)
{
nombre1.Text= NOMPROPIO(nombre1.Text);
}

string NOMPROPIO(string s)
{
  if (String.IsNullOrEmpty(s))
     s = "";
  string phrase = "";
  string[] words = s.Split(' ');
  foreach (string word in words)
  {
    if (word.Length > 1)
    phrase += word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + " ";
    else
    phrase += word.ToUpper() + " ";
  }
return phrase.Trim();
}