I have a Comma separated Upper case string with spaces that needs to be converted into a comma separated string in Title Case and trim the end spaces only.
Sample String:
"BAHAMAS, BAHRAIN, BANGLADESH, BONAIRE SINT EUSTATIUS, BOSNIA HERZEGOVINA"
Desired String:
"Bahamas,Bahrain,Bangladesh,Bonaire Sint Eustatius,Bosnia Herzegovina"
The code that I tried:
string s_Response = responseString.Split(',')
.Select(x=>x.Trim())
.Select(y=>y.Join(" ",y.Split(' ').Select(i=>i.Substring(0,1).ToUpper()+i.Substring(1).ToLower()).ToArray()))
This however gives me an error for Join (qualify it with a type name instead)
I seek help for setting the logic of array comparisons
Any help is appreciated 🙂
2
Answers
TextInfo.ToTitleCase(String) Method – Converts the specified string to title case (except for words that are entirely in uppercase, which are considered to be acronyms).
String.Replace
will help with spaces:When all else fails, there’s always a good ol’ fashioned state machine:
Note that the
GetCharacterState
function is a local function (it gets it out of the main program flow, but keeps it with the main function.If you want to have a list of "noise works" (of, the, etc.), you could easily implement it. The code is not simple, but it is pretty easy to read, and it is
O(N)
.Test output from the stated input:
Bahamas,Bahrain,Bangladesh,Bonaire Sint Eustatius,Bosnia Herzegovina