2009-07-22 12 views

Respuesta

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

(tenga en cuenta que sólo funciona inferior a superior, por lo tanto, a partir minúscula)

+0

¿Eso está ahí? Oh mi. +1 –

+0

@Marc: ¿'ToTitleCase()' maneja correctamente "Peter O'Toole" y "Mary Jones-Smith"? –

+0

@Grant: Peter necesita un nuevo nombre, aunque Mary está bien. –

0
public static string CamelCase(this 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(); 
    } 
+0

@TruthStands: no produce los resultados correctos para "Peter O'Toole" y "Mary Smith-Jones". –

+0

Cierto, pero no sería difícil arreglar eso. – TruthStands

0

He votado en la respuesta de Marc, pero esto también funcionará:

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

Deberá agregar las referencias adecuadas, pero estoy bastante seguro de que funciona en las entradas superiores.

Cuestiones relacionadas