System.Console.WriteLine(char.ToUpper(str[0]) + str.Substring(1));
// first letter of string
private string CapitalizeFirst (string input) {
char.ToUpper(myString[0]) + myString[1..]; // captalizes first letter
}
// first letter of each word
private string CapitalizeAll (string input) {
var words = input.Split(' '); // create array of words
// loop through all words and capitalize them
for (int i = 0; i < words.length; i++) {
words[i] = CapitalizeFirst (words[i]);
}
return string.Join(' ', words);
}
string myString = "hello world";
// results
CapitalizeFirst(myString); // "Hello world"
CapitalizeAll(myString); // "Hello World"
public static class StringExtension
{
public static string CapitalizeFirst(this string s)
{
bool IsNewSentense = true;
var result = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; i++)
{
if (IsNewSentense && char.IsLetter(s[i]))
{
result.Append (char.ToUpper (s[i]));
IsNewSentense = false;
}
else
result.Append (s[i]);
if (s[i] == '!' || s[i] == '?' || s[i] == '.')
{
IsNewSentense = true;
}
}
return result.ToString();
}
}