Let us say the following is your string −
String str = "Welcome to our website!";
Create a char array of the string included above using the ToCharArray() method:
char []ch = str.ToCharArray();
To convert the first character to uppercase −
if (ch[i] >= 'a' && ch[i] <= 'z') {
// Convert into Upper-case
ch[i] = (char)(ch[i] - 'a' + 'A');
}Example
You can try to run the following code to convert first character uppercase in a sentence.
using System;
class Demo {
static String MyApplication(String str) {
char []val = str.ToCharArray();
for (int i = 0; i < str.Length; i++) {
if (i == 0 && val[i] != ' ' ||
val[i] != ' ' && val[i - 1] == ' ') {
if (val[i] >= 'a' && val[i] <= 'z') {
val[i] = (char)(val[i] - 'a' + 'A');
}
} else if (val[i] >= 'A' && val[i] <= 'Z')
val[i] = (char)(val[i] + 'a' - 'A');
}
String s = new String(val);
return s;
}
public static void Main() {
String str = "Welcome to our website!";
Console.Write(MyApplication(str));
}
}Output
Welcome To Our Website!