String conversion, first character of the upper word

I want to convert:

HECHT, WILLIAM 

to

 Hecht, William 

in c #.

any nifty ways to do this?

+7
string c #
source share
4 answers
 string name = "HECHT, WILLIAM"; string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower()); 

(note that it only works from lower to upper, therefore, starting from lower case)

+31
source share

I would like to include an answer that indicates that, although this seems simple in theory, in practice the correct capitalization of each name can be very complex:

In any case, there’s just something to think about.

+4
source share
  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
source share

I voted for Mark's answer, but this will also work:

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

You will need to add the appropriate links, but I'm sure it works on all the top inputs.

0
source share

All Articles