Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string conversion, first character upper of each word

Tags:

string

c#

i want to convert:

HECHT, WILLIAM 

to

Hecht, William

in c#.

any elegant ways of doing this?

like image 237
leora Avatar asked Jul 22 '09 21:07

leora


People also ask

What converts the first letter of each word into the uppercase?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

How do you capitalize the first character of each word in a string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.

Which string method converts first character of a string to upper case?

The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

How do you convert the first character of each element in a series to uppercase?

Pandas – Convert the first and last character of each word to upper case in a series. In python, if we wish to convert only the first character of every word to uppercase, we can use the capitalize() method. Or we can take just the first character of the string and change it to uppercase using the upper() method.


2 Answers

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

(note it only works lower-to-upper, hence starting lower-case)

like image 118
Marc Gravell Avatar answered Oct 24 '22 17:10

Marc Gravell


I'd just like to include an answer that points out that although this seems simple in theory, in practice properly capitalizing the names of everyone can be very complicated:

  • Peter O'Toole
  • Xavier Sala-i-Martin
  • Salvador Domingo Felipe Jacinto Dalí i Domènech
  • Francis Sheehy-Skeffington
  • Asma al-Assad
  • Maggie McIntosh
  • Vincent van Gogh

Anyway, just something to think about.

like image 35
Grant Wagner Avatar answered Oct 24 '22 16:10

Grant Wagner