using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; namespace LeafWeb.Core.Utility { public static class StringExtensions { public static string SplitCamelCase(this string str) { return str.Aggregate( string.Empty, (current, c) => current + (char.IsUpper(c) && current.Length > 0 ? " " + c : c.ToString(CultureInfo.InvariantCulture))); } public static string LowercaseFirst(string s) { // Check for empty string. if (string.IsNullOrEmpty(s)) { return string.Empty; } // Return char and concat substring. return char.ToLowerInvariant(s[0]) + s.Substring(1); } public static byte[] GetBytes(this string str) { var bytes = new byte[str.Length * sizeof(char)]; Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } public static string GetString(this byte[] bytes) { var chars = new char[bytes.Length / sizeof(char)]; Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); } public static Dictionary SplitConnectionString(this string connectionString) { return connectionString.Split(';') .Select(t => t.Split(new[] { '=' }, 2)) .ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase); } public static string FilterAlphaNumeric(this string input) { return Regex.Replace(input, @"[^\w_]", ""); } } }