ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

ToFirstAll

This method makes the caps for all words in a string

Source

public static string ToFirstAll(this string input, bool switcher)
{
            return new string(input.Split(' ').
                Select(n => switcher ? (n.ToArray().First().ToString().ToUpper() + n.Substring(1, n.Length - 1)) :
                    (n.ToArray().First().ToString().ToLower() + n.Substring(1, n.Length - 1))).
                    Aggregate((a, b) => a + ' ' + b).ToArray()).TrimEnd(' ');
}

Example

string result_string = "hello world!".ToFirstAll(true);
MessageBox.Show(result_string);

Author: Denis Stoyanov

Submitted on: 25 okt 2010

Language: C#

Type: System.String

Views: 3949