I have a string like this LUXOR and I want to convert other letters to lowercase except first letter or the string.that meant, I want this string Luxor from above string. I can convert full string to upper or lower by using ToUpper or ToLower.but how can I do this.hope your help with this.thank you
Add a comment
|
2 Answers
You can make use of TextInfo class that Defines text properties and behaviors, such as casing, that is specific to a writing system.
string inString = "LUXOR".ToLower();
TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
string output = cultInfo.ToTitleCase(inString);
This snippet will give you
Luxorin the variableoutput. this can also be used to capitalize Each Words First Letter
Another option is using .SubString, for this particular scenario of having a single word input:
string inString = "LUXOR"
string outString = inString.Substring(0, 1).ToUpper() + inString.Substring(1).ToLower();
6 Comments
Archimaredes
A little bit of context/explanation might be nice! +1 for the script/language-independent solution, though.
Anders Tornblad
Your original answer will actually NOT work for ALL UPPERCASE texts, because these are exempt from the conversion ; they are seen as abbreviations!
"this is FBI calling" will render "This Is FBI Calling". Your updated answer is a bit too complex for the simple task requested.Anders Tornblad
Also, please don't link the
TextInfo word to some blog. Link to MSDN directly.sujith karivelil
@AndersTornblad: thanks for the valuable point, I have updated the answer with
string inString = "LUXOR".ToLower(); and also corrected the linkChris Dunaway
@Graham'anu' - Beware that in the second one, you are creating 4 extra strings (Substring x 2) and (ToLower x 2)
|
Try This,
string inString = "LUXOR";
string output = inString.Substring(0, 1) + inString.Substring(1).ToLower();
string inString2 = "HI HOW ARE YOU";
string[] finalstring = inString2.Split(' ');
string output2 = string.Empty;
foreach (var item in finalstring)
{
if (output2 == "")
{
output2 = (item.ToUpper().Substring(0, 1) + item.ToLower().Substring(1));
}
else
{
output2 += " " + (item.ToUpper().Substring(0, 1) + item.ToLower().Substring(1));
}
}