2009-07-22 8 views

risposta

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

(nota funziona solo inferiore a superiore, quindi a partire minuscole)

+0

È quella cosa lì dentro? Oh mio. +1 –

+0

@Marc: 'ToTitleCase()' gestisce correttamente "Peter O'Toole" e "Mary Jones-Smith"? –

+0

@Grant: Peter ha bisogno di un nuovo nome, Mary sta bene però. –

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: non produce i risultati corretti per "Peter O'Toole" e "Mary Smith-Jones". –

+0

Vero, ma non sarebbe difficile risolverlo. – TruthStands

0

ho votato la risposta di Marc, ma questo sarà anche funzionare:

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

Dovrai aggiungere i riferimenti appropriati, ma sono abbastanza sicuro che funzioni su tutti gli input superiori.

Problemi correlati