How to make a first letter capital in C#

后端 未结 12 1165
攒了一身酷
攒了一身酷 2020-12-05 23:52

How can the first letter in a text be set to capital?

Example:

it is a text.  = It is a text.
相关标签:
12条回答
  • 2020-12-06 00:04

    I realize this is an old post, but I recently had this problem and solved it with the following method.

        private string capSentences(string str)
        {
            string s = "";
    
            if (str[str.Length - 1] == '.')
                str = str.Remove(str.Length - 1, 1);
    
            char[] delim = { '.' };
    
            string[] tokens = str.Split(delim);
    
            for (int i = 0; i < tokens.Length; i++)
            {
                tokens[i] = tokens[i].Trim();
    
                tokens[i] = char.ToUpper(tokens[i][0]) + tokens[i].Substring(1);
    
                s += tokens[i] + ". ";
            } 
    
            return s;
        }
    

    In the sample below clicking on the button executes this simple code outBox.Text = capSentences(inBox.Text.Trim()); which pulls the text from the upper box and puts it in the lower box after the above method runs on it.

    Sample Program

    0 讨论(0)
  • 2020-12-06 00:10

    polygenelubricants' answer is fine for most cases, but you potentially need to think about cultural issues. Do you want this capitalized in a culture-invariant way, in the current culture, or a specific culture? It can make a big difference in Turkey, for example. So you may want to consider:

    CultureInfo culture = ...;
    text = char.ToUpper(text[0], culture) + text.Substring(1);
    

    or if you prefer methods on String:

    CultureInfo culture = ...;
    text = text.Substring(0, 1).ToUpper(culture) + text.Substring(1);
    

    where culture might be CultureInfo.InvariantCulture, or the current culture etc.

    For more on this problem, see the Turkey Test.

    0 讨论(0)
  • 2020-12-06 00:14

    Try this code snippet:

    char nm[] = "this is a test";
    
    if(char.IsLower(nm[0]))  nm[0] = char.ToUpper(nm[0]);
    
    //print result: This is a test
    
    0 讨论(0)
  • 2020-12-06 00:15

    I use this variant:

     private string FirstLetterCapital(string str)
            {
                return Char.ToUpper(str[0]) + str.Remove(0, 1);            
            }
    
    0 讨论(0)
  • static String UppercaseWords(String BadName) { String FullName = "";

            if (BadName != null)
            {
                String[] FullBadName = BadName.Split(' ');
                foreach (string Name in FullBadName)
                {
                    String SmallName = "";
                    if (Name.Length > 1)
                    {
                        SmallName = char.ToUpper(Name[0]) + Name.Substring(1).ToLower();
                    }
                    else
                    {
                        SmallName = Name.ToUpper();
                    }
                    FullName = FullName + " " + SmallName;
                }
    
            }
            FullName = FullName.Trim();
            FullName = FullName.TrimEnd();
            FullName = FullName.TrimStart();
            return FullName;
        }
    
    0 讨论(0)
  • 2020-12-06 00:19

    If you are using C# then try this code:

    Microsoft.VisualBasic.StrConv(sourceString, Microsoft.VisualBasic.vbProperCase)
    
    0 讨论(0)
提交回复
热议问题