How to make a first letter capital in C#

后端 未结 12 1196
攒了一身酷
攒了一身酷 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条回答
  •  旧时难觅i
    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

提交回复
热议问题