Last active
August 29, 2015 14:25
-
-
Save hyrmn/836675eb85e773179458 to your computer and use it in GitHub Desktop.
Goal: In C#, convert a string to lower case. e.g. "TEST STRING" becomes "test string"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = s.ToLower(); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = s.Aggregate(string.Empty, (current, c) => current + char.ToLower(c)); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var lowerMethod = typeof (string).GetMethods().First(m => m.Name == "ToLower"); | |
| var lower = lowerMethod.Invoke("TEST STRING", null).ToString(); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = string.Empty; | |
| for (var i = 0; i < s.Length; i++) | |
| { | |
| lower += char.ToLower(s[i]); | |
| } | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = string.Empty; | |
| s.ToCharArray().ToList().ForEach(c => lower += c.ToString().ToLower()); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class TheHelpers | |
| { | |
| public static char ToLower(this char c) | |
| { | |
| return char.ToLower(c); | |
| } | |
| public static IEnumerable<K> MapEach<T, K>(this IEnumerable<T> collection, Func<T, K> map) | |
| { | |
| return collection.Select(map); | |
| } | |
| public static string Join(this IEnumerable<char> chars, string seperator) | |
| { | |
| var en = chars.GetEnumerator(); | |
| var sb = new StringBuilder(); | |
| if (en.MoveNext()) | |
| sb.Append(en.Current); | |
| while (en.MoveNext()) | |
| sb.Append(seperator).Append(en.Current); | |
| return sb.ToString(); | |
| } | |
| } | |
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = s.MapEach(c => c.ToLower()).Join(""); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var lower = "TEST STRING".Aggregate(string.Empty, (current, c) => current + (char)(c + 32) ).Replace('@', ' '); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static readonly IDictionary<Type, Func<object, string>> _poorExcuseForAbstractFactory = new Dictionary<Type, Func<object, string>> | |
| { | |
| {typeof(string), o => o.ToString().ToLower()} | |
| }; | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = _poorExcuseForAbstractFactory[s.GetType()](s); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Program | |
| { | |
| private static void Main() | |
| { | |
| var s = "TEST STRING"; | |
| var lower = RecursivelyLowercaseThatShizzle(s); | |
| Debug.Assert(lower == "test string"); | |
| } | |
| public static string RecursivelyLowercaseThatShizzle(string s, int currentCharacterToProcess = 0) | |
| { | |
| if (currentCharacterToProcess < s.Length - 1) | |
| { | |
| return s.Substring(currentCharacterToProcess, 1).ToLower() + RecursivelyLowercaseThatShizzle(s, ++currentCharacterToProcess); | |
| } | |
| return s.Substring(currentCharacterToProcess, 1).ToLower(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I feel like I need to add something that has an IAbstractHelloWorldFactoryFactory feel to it but I don't want to jump the shark.