Resolved Manage word (UPPER, lower)

PsychoNINJA

Member
Joined
Sep 18, 2021
Messages
12
Programming Experience
1-3
Hello dear Community,
I have a question, I want to get a string parameter in any type of character (upper or lower case) and i would like to turn it to upper case only on first character and others just lower case.

For example:
My parameter is FERrarI or FERRARI
I would like to convert it to just Ferrari

Is it possible? any code please.
 
Solution
If you want title case, i.e. the first letter of each word in upper case and the rest lower, then you can do this:
C#:
str = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
If you're always going to have one word then that would fulfil your requirement. Here's an extension method:
C#:
using System.Globalization;

namespace MyProject
{
    public static class StringExtensions
    {
        public static string ToTitle(this string source)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source);
        }
    }
}
that would allow you to do this:
C#:
str = str.ToTitle();
much as you would use ToUpper or ToLower. If you might have multiple words but only want the very first character in...
If you want title case, i.e. the first letter of each word in upper case and the rest lower, then you can do this:
C#:
str = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
If you're always going to have one word then that would fulfil your requirement. Here's an extension method:
C#:
using System.Globalization;

namespace MyProject
{
    public static class StringExtensions
    {
        public static string ToTitle(this string source)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source);
        }
    }
}
that would allow you to do this:
C#:
str = str.ToTitle();
much as you would use ToUpper or ToLower. If you might have multiple words but only want the very first character in upper case then you need to treat that character separately to the rest:
C#:
str = char.ToUpper(str[0]) + str.Substring(1).ToLower();
 
Solution
How will that handle words like?
C#:
'tis
'twas
'cello
'cause
 

Latest posts

Back
Top Bottom