ToUpper and ToLower first char and first word

StewartBW 705 Reputation points
2024-05-16T13:29:08.5166667+00:00

Hello,

Going to convert the first letter of each word to upper and lower case, to upper is simple, but to lower?

System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(---)

And to convert the first word of string to upper and lower case, no methods in .net?

Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,521 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,636 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 27,746 Reputation points Microsoft Vendor
    2024-05-17T01:22:47.89+00:00

    Hi @StewartBW ,

    You can write one yourself, referring to the function below.

        Function ConvertToFirstLetterLowerCase(input As String) As String
            If String.IsNullOrWhiteSpace(input) Then
                Return input
            End If
    
            Dim words As String() = input.Split(" "c)
    
            For i As Integer = 0 To words.Length - 1
                If words(i).Length > 0 Then
                    words(i) = Char.ToLower(words(i)(0)) & words(i).Substring(1)
                End If
            Next
    
            Return String.Join(" ", words)
        End Function
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Karen Payne MVP 35,286 Reputation points
    2024-05-17T08:13:57.3033333+00:00

    The following extension method lower cases the first character to lower.

    public static class StringExtensions
    {
        public static string? FirstCharToLowerCase(this string? sender)
        {
            if (!string.IsNullOrEmpty(sender) && char.IsUpper(sender[0]))
                return sender.Length == 1 ? 
                    char.ToLower(sender[0]).ToString() : 
                    char.ToLower(sender[0]) + sender[1..];
    
            return sender;
        }
    }
    
    0 comments No comments