Sunday, November 25, 2012

C# Uppercase First Letter

Strings sometimes have lowercase first letters. Uppercasing the first letter is often necessary. The string has its first letter uppercased. It is then returned with the remaining part unchanged. This is the functionality of the ucfirst function from PHP and Perl. And strings with multiple words can be changed to title case.
Input and output

Input:  samuel
 julia
 john smith

Output: Samuel
 Julia
 John smith [only first letter]

Examples

First, the goal of these methods is to return correctly uppercased strings in an efficient way. We provide an example of a C# method that uppercases the first letter. The method first here is the slower of the two. The benchmark is presented after the two programs.
Program that uppercases words [C#]

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
Next,we look at a method that uses the ToCharArray method instead. I investigated ToCharArray and its performance impact on this sort of character-based method. The following is the faster version, and it returns the same results.
Another program that uppercases [C#]

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
Performance optimization
Here are my resultsfrom testing 1,000,000 iterations of these functions in a loop over the string "michael". This test is in my ASP.NET application, so the environment is the same as it will be in my ASP.NET app as it is updated on the Internet.
Benchmark results

UppercaseFirst method:           0.1636 seconds
UppercaseFirst with ToCharArray: 0.0920 seconds [faster]
The second approachis faster because it only allocates one new string in the return statement. The first approach allocates two strings: the Substring(1), and then a new string with string.Concat.
ReturnSubstringConcat

Uppercase words

Programming tip
This program defines a method named UppercaseWords that is equivalent to the ucwords function in scripting languages such as PHP. The UppercaseWords method internally converts the string to a character array buffer.The char array data is mutable,which means you can change it without reallocating or filling up the managed heap. The method mutates the character buffer and then returns a new string from the string constructor based on the mutated buffer.
Program that uppercases first letters [C#]

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
Note
The program definesthe UppercaseWords static method, which is equivalent to the ucwords function in scripting languages. Internally, the method body of the UppercaseWords method first tests the first character of the string.
Note:It special-cases the first character because the loop in the next part does not correctly allow for the very first character. The if-statement is validated in the test cases when the first two strings have their first letters uppercased.
IfFor loop
For-loop.The next part of the UppercaseWords method does a for-loop through all the characters in the string starting at the second character with index of one. In each iteration it tests the previous character for a space. If it detects a space, it modifies the character array buffer to have an uppercase character.
ForSplit strings
Efficiency notes.The UppercaseWords method could be implemented by first splitting on the sub-words in the string based on a regular expression or the Split method. But this would result in many more allocations—one for the array reference data and one for each word.
Split
However:You could use the Regex.Split method for a better algorithm of breaking up the words.
Regex.Split
Note:The method does not contain sophisticated error correction and does not handle some words—such as and, the, and a—differently.

ToTitleCase


You might not be in the mood for deploying a custom method for uppercasing the first letter in each word in a string. You can use the ToTitleCase method on the TextInfo type—it does the same thing. Using ToTitleCase will make handling certain special cases harder.
ToTitleCase

Summary

The C# programming language
We looked at methods that uppercase the first character in strings using the C# language. They are useful for database results that may not be formatted correctly. Custom logic for certain names can be added.
Normally:The performance requirements are not strict here.
But:Occasionally the benchmarks here may be useful. We can capitalize also strings that have multiple words.

=======================================================
For more Books / Ads / Lost-Found / Articles / Accommodation. We can help with your busy lifestyle. Take a look around and review our hundreds of free Ads or submit one of your own favorites. http://www.inhousetoday.com

No comments: