Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert first letter in string to uppercase

Tags:

c++

string

mbcs

I have a string: "apple". How can I convert only the first character to uppercase and get a new string in the form of "Apple"?

I can also have a string with multibyte characters.

What if the first character of the string is a multibyte character ?

like image 382
user1065276 Avatar asked Dec 16 '11 06:12

user1065276


People also ask

How do you convert the first letter of string to uppercase in TypeScript?

To capitalize the first letter of a string in TypeScript:Use the charAt() method to get the first letter of the string. Call the toUpperCase() method on the letter. Use the slice() method to get the rest of the string. Concatenate the results.

How do you uppercase the first letter of a string in Python?

How do you capitalize the first letter of a string? The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.

How do I convert first letter to uppercase in Excel?

In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead. Use =UPPER(A2) in cases where you need to convert text to uppercase, replacing A2 with the appropriate cell reference.


2 Answers

string str = "something"; str[0] = toupper(str[0]); 

That's all you need to do. It also works for C strings.

like image 200
Seth Carnegie Avatar answered Oct 05 '22 22:10

Seth Carnegie


Like what Carneigie said,

string str = "something"; str[0] = toupper(str[0]); 

but also remember to:

#include <string> #include <cctype> 

all the way up

like image 39
HoKy22 Avatar answered Oct 05 '22 23:10

HoKy22