/* * GNU AFFERO GENERAL PUBLIC LICENSE * Version 3, 19 November 2007 * Copyright (C) 2007 Free Software Foundation, Inc. * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ using System; using System.ComponentModel; using System.Linq; namespace UVtools.Parser.Extensions { public static class StringExtensions { /// /// Upper the first character in a string /// /// Input string /// Modified string with fist character upper public static string FirstCharToUpper(this string input) { switch (input) { case null: throw new ArgumentNullException(nameof(input)); case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)); default: return input.First().ToString().ToUpper() + input.Substring(1); } } /// /// Converts a string into a target type /// /// Target type to convert into /// Value /// Converted value into target type public static T Convert(this string input) { var converter = TypeDescriptor.GetConverter(typeof(T)); if (converter != null) { //Cast ConvertFromString(string text) : object to (T) return (T)converter.ConvertFromString(input); } return default(T); } } }