C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Words: Strings with multiple words can be changed to title case. We can capitalize "Multiple Words."
C# program that uppercases words
using System;
class Program
{
static void Main()
{
Console.WriteLine(UppercaseFirst("samuel"));
Console.WriteLine(UppercaseFirst("julia"));
Console.WriteLine(UppercaseFirst("john smith"));
}
static string UppercaseFirst(string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
}
Output
Samuel
Julia
John smith
C# program that uses ToCharArray, uppercases
using System;
class Program
{
static void Main()
{
Console.WriteLine(UppercaseFirst("samuel"));
Console.WriteLine(UppercaseFirst("julia"));
Console.WriteLine(UppercaseFirst("john smith"));
}
static string UppercaseFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
}
Output
Samuel
Julia
John smith
Info: The second approach is faster because it only allocates one new string in the return statement.
Also: The first approach allocates two strings: the Substring(1), and then a new string with string.Concat.
ReturnSubstringstring.ConcatBenchmark results
UppercaseFirst method: 0.1636 seconds
UppercaseFirst with ToCharArray: 0.0920 seconds [faster]
UppercaseWords: Internally, the method body of this method first tests the first character of the string.
And: It special-cases the first character because the loop in the next part does not correctly allow for the first character.
For: UppercaseWords does a for-loop through all the characters in the string starting at the second character with index of one.
ForNote: In each iteration it tests the previous character for a space. If it detects a space, it modifies the character array buffer.
C# program that uppercases first letters
using System;
class Program
{
static string UppercaseWords(string value)
{
char[] array = value.ToCharArray();
// Handle the first letter in the string.
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
static void Main()
{
// Uppercase words in these strings.
const string value1 = "something in the way";
const string value2 = "dot net PERLS";
const string value3 = "String_two;three";
const string value4 = " sam";
// ... Compute the uppercase strings.
Console.WriteLine(UppercaseWords(value1));
Console.WriteLine(UppercaseWords(value2));
Console.WriteLine(UppercaseWords(value3));
Console.WriteLine(UppercaseWords(value4));
}
}
Output
Something In The Way
Dot Net PERLS
String_two;three
Sam
However: You could use the Regex.Split method for a better algorithm of breaking up the words.
Regex.SplitNote: The method does not contain sophisticated error correction. It may incorrectly handle some words.