guinness Posted March 17, 2014 Share Posted March 17, 2014 (edited) Here is the namespace called CustomMethod with the class called Extensions. It's to add additional functionality to the likes of in-built datatypes such as string. Use the same way as you would when calling the likes of "myString".ToUpper() or 'c'.ToString() etc... expandcollapse popup/* * Created by guinness * User: guinness * Date: 05/04/2014 */ using System; using System.Text.RegularExpressions; namespace CustomMethod { public static class Extensions // Create a custom class for extension methods. { /// <summary> /// Checks whether a character is a digit from 0-9. /// </summary> /// <param name="c">A character value.</param> /// <returns>True or False</returns> public static bool IsDigit(this char c) { return (c >= '0' && c <= '9'); } /// <summary> /// Checks whether a string is a digits from 0-9. /// </summary> /// <param name="i">A string value.</param> /// <returns>True or False</returns> public static bool IsDigit(this string i) { foreach (char value in i) { if (value < '0' || value > '9') return false; } return true; } /// <summary> /// Checks whether a double is an even value. /// </summary> /// <param name="d">A double value.</param> /// <returns>True or False</returns> public static bool IsEven(this double d) { return Math.Round(d) % 2 == 0; } /// <summary> /// Checks whether an integer is an even value. /// </summary> /// <param name="i">An integer value.</param> /// <returns>True or False</returns> public static bool IsEven(this int i) { return i % 2 == 0; } /// <summary> /// Checks whether a character is a hexadecimal value from 0-9 and A-F. /// </summary> /// <param name="c">A character value.</param> /// <returns>True or False</returns> public static bool IsHex(this char c) { return (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'); } /// <summary> /// Checks whether a string is a hexadecimal value from 0-9 and A-F. /// </summary> /// <param name="s">A string value.</param> /// <returns>True or False</returns> public static bool IsHex(this string s) { foreach (char value in s) { if (value < '0' || value > '9' && value < 'A' || value > 'F' && value < 'a' || value > 'f') return false; } return true; } /// <summary> /// Checks if a double is an odd value. /// </summary> /// <param name="d">A double value.</param> /// <returns>True or False</returns> public static bool IsOdd(this double d) { return Math.Round(d) % 2 == 1; } /// <summary> /// Checks if an integer is an odd value. /// </summary> /// <param name="i">An integer value.</param> /// <returns>True or False</returns> public static bool IsOdd(this int i) { return i % 2 == 1; } /// <summary> /// Checks whether a string is a palindrome. /// </summary> /// <param name="s">A string value.</param> /// <returns>True or False</returns> public static bool IsPalindrome(this string s) { s = Regex.Replace(s, @"\W", "").ToLower(); // Remove all non-word characters and convert to lowercase. char[] c = s.ToCharArray(); Array.Reverse(c); return new string(c) == s; } /// <summary> /// Checks if a byte value is true when non-zero. (Like in most languages.) /// </summary> /// <param name="b">A byte value.</param> /// <returns>True or False</returns> public static bool IsTrue(this byte b) { return b != 0; } /// <summary> /// Checks if a double value is true when non-zero. (Like in most languages.) /// </summary> /// <param name="d">A double value.</param> /// <returns>True or False</returns> public static bool IsTrue(this double d) { return d != 0; } /// <summary> /// Checks if an integer value is true when non-zero. (Like in most languages.) /// </summary> /// <param name="i">An integer value.</param> /// <returns>True or False</returns> public static bool IsTrue(this int i) { return i != 0; } /// <summary> /// Checks if a string is true when the value is not empty. (Like in most languages.) /// </summary> /// <param name="s">A string value.</param> /// <returns>True or False</returns> public static bool IsTrue(this string s) { return s.Length != 0; } /// <summary> /// Repeat a character a certain number of times. /// </summary> /// <param name="c">A character value.</param> /// <param name="count">Number of times to repeat the character.</param> /// <returns>Repeated character.</returns> public static string Repeat(this char c, int count) { return (count < 0) ? c.ToString() : new string(c, count); } /// <summary> /// Repeat a string a certain number of times. /// </summary> /// <param name="s">A string value.</param> /// <param name="count">Number of times to repeat the string.</param> /// <returns>Repeated string.</returns> public static string Repeat(this string s, int count) // Idea taken from AutoIt _StringRepeat() { if (count < 0 || s.Length < 1) // Check the string length is greater than 1 and the repeat count is valid. Zero is considered valid. return s; return string.Join(s, new string[count + 1]); // http://rosettacode.org/wiki/Repeat_a_string#C.23 } /// <summary> /// Reverse a string. /// </summary> /// <param name="s">A string value.</param> /// <returns>Reversed string.</returns> public static string Reverse(this string s) { char[] c = s.ToCharArray(); int i = 0, j = c.Length - 1; while (i < j) // This version is a slightly optimised version than Array.Reverse(). { char temp = c[i]; c[i++] = c[j]; c[j--] = temp; } return new string(c); /* char[] c = s.ToCharArray(); Array.Reverse(c); return new string(c); */ } /// <summary> /// Convert an integer to a hexadecimal representation. /// </summary> /// <param name="i">An integer value.</param> /// <param name="isLowerCase">Is the representation lowercase (true) or uppercase (false).</param> /// <returns>Hexadecimal string.</returns> public static string ToHexString(this int i, bool isLowerCase = true) // Convert an integer to a hex string representation. { return i.ToString((isLowerCase) ? "x" : "X"); } private static string ToLowerFast(this string s) // (Faster) convert a string to lowercase characters. Access the same way as .ToLower(). { string lowerString = string.Empty; foreach (char value in s) { lowerString += (char)((value >= 'A' && value <= 'Z') ? (value + 32) : value); // Cast as a char. } return lowerString; } private static string ToUpperFast(this string s) // (Faster) convert a string to uppercase characters. Access the same way as .ToUpper(). { string upperString = string.Empty; foreach (char value in s) { upperString += (char)((value >= 'a' && value <= 'z') ? (value - 32) : value); // Cast as a char. } return upperString; } } } Note: Improved version found >here. Edited January 11, 2015 by guinness FaridAgl 1 UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted March 17, 2014 Author Share Posted March 17, 2014 (edited) Example:/* * Created by guinness * User: guinness * Date: 15/03/2014 * Time: 11:42 * */ using System; using CustomMethod; namespace Example { internal class Program { public static void Main() { Console.WriteLine("Reversed string: {0}", "reverse".Reverse()); Console.WriteLine("Repeated string: {0}", "s".Repeat(20)); Console.WriteLine("Is Odd: {0}", 100.IsOdd()); // Is the number odd? Console.WriteLine("Is Odd: {0}", 99.IsOdd()); // Is the number odd? Console.WriteLine("Is Even: {0}", 99.IsEven()); // Is the number even? Console.WriteLine("To Hex string: {0}", 255.ToHexString(false)); Console.WriteLine("Is char digit: {0}", 'F'.IsDigit()); Console.WriteLine("Is Hex: {0}", "FF00FF".IsHex()); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } } Edited March 18, 2014 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Richard Robertson Posted March 18, 2014 Share Posted March 18, 2014 i.ToString("x") would have been easier than using string.Format. Link to comment Share on other sites More sharing options...
guinness Posted March 18, 2014 Author Share Posted March 18, 2014 True, not to mention I am missing XML comments. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted March 18, 2014 Author Share Posted March 18, 2014 (edited) Updated with XML comments. Suggestions or just pointing out a blatant disregard for standards and practices is much appreciated too. By the way it might be overkill but have you checked out this? >> http://zextensionmethods.codeplex.com/ Edited March 18, 2014 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Richard Robertson Posted March 19, 2014 Share Posted March 19, 2014 http://msdn.microsoft.com/en-us/library/7f0ddtxh%28v=vs.110%29.aspx Link to comment Share on other sites More sharing options...
guinness Posted April 5, 2014 Author Share Posted April 5, 2014 Updated the code in the first post with a couple of additions such as IsTrue() and IsPalindrome(). UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
jaberwacky Posted April 7, 2014 Share Posted April 7, 2014 (edited) public static bool IsDigit(this char c) { return (c >= '0' && c <= '9'); } I don't use C# so I don't know. C# being a strongly typed language, are the ASCII values compared in this case? According to wikipedia at least, "C# disallows this "integer meaning true or false" approach, on the grounds that forcing programmers to use expressions that return exactly bool can prevent certain types of programming mistakes common in C or C++ such as if (a = b) (use of assignment = instead of equality ==)". So your IsTrue methods may be anti-patterns. I'm no expert so, meh. Edited April 7, 2014 by jaberwacky Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
guinness Posted April 7, 2014 Author Share Posted April 7, 2014 The char type in this instance is compared by its numerical value. So if c = '1' then it's saying 49 >= 48 AND 49 <= 57. Understand? The IsTrue() extensions are only there as proof of concept. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Mat Posted April 7, 2014 Share Posted April 7, 2014 Why not just Char.IsDigit©? Also, does your IsOdd function work with negatives? I haven't tried and different languages have different meaning of mod, but a more universal way to tell if a number is odd is to use N % 2 != 0, as -3 % 2 might be 1 or -1 depending on the implementation. AutoIt Project Listing Link to comment Share on other sites More sharing options...
guinness Posted April 7, 2014 Author Share Posted April 7, 2014 Why not just Char.IsDigit©? Also, does your IsOdd function work with negatives? I haven't tried and different languages have different meaning of mod, but a more universal way to tell if a number is odd is to use N % 2 != 0, as -3 % 2 might be 1 or -1 depending on the implementation. I could have used IsDigit, but was just loosing the call overhead. IsOdd() didn't work with negative values, but now it does. Thanks. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 9, 2015 Author Share Posted January 9, 2015 (edited) A new version. I also added methods which resemble those found in the String Management section e.g. TrimLeft and TrimRight etc...expandcollapse popup#region Required project assemblies using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; #endregion /* * Created by guinness * User: guinness * Date: 11/01/2015 */ namespace ExtensionMethods { // The following links were used to aid in the creation of these extension methods: // http://extensionoverflow.codeplex.com/SourceControl/latest#ExtensionMethods/ // http://blog.sortedbits.com/string-extension-methods-for-c-2/ // http://extensionmethod.net/csharp/string // https://www.autoitscript.com/autoit3/docs/functions/String%20Management.htm #pragma warning disable 1591 public enum Cipher : byte { Rot1, Rot2, Rot3, Rot4, Rot5, Rot6, Rot7, Rot8, Rot9, Rot10, Rot11, Rot12, Rot13, Rot14, Rot15, Rot16, Rot17, Rot18, Rot19, Rot20, Rot21, Rot22, Rot23, Rot24, Rot25 } #pragma warning restore 1591 /// <summary> /// ISBN related flags. /// </summary> public enum ISBN : byte { /// <summary> /// 10-digit ISBN. /// </summary> Ten, /// <summary> /// 13-digit ISBN. /// </summary> Thirteen } /// <summary> /// Flags related to between. /// </summary> public enum StringBetween : byte { /// <summary> /// The end string at the end of a match starts the next possible match. /// </summary> None = 0, /// <summary> /// A further instance of the start starts the next match. /// </summary> EndIsStart = 1 } /// <summary> /// Flags related to stripping white space. /// </summary> [Flags] // http://hugoware.net/blog/enums-flags-and-csharp public enum StringStripWS : byte { /// <summary> /// Strip leading white space /// </summary> Leading = 1 << 0, /// <summary> /// Strip trailing white space /// </summary> Trailing = 1 << 1, /// <summary> /// Strip double (or more) spaces between words /// </summary> Spaces = 1 << 2, /// <summary> /// Strip all spaces (over-rides all other flags) /// </summary> All = 1 << 3 } /// <summary> /// Extension methods related to System.Object. /// </summary> public static class ObjectIs { /// <summary> /// Checks if an object is not null. /// </summary> /// <param name="value">A object to check.</param> /// <returns>true if object was not null; otherwise, false.</returns> public static bool IsNotNull(this object value) { return value != null; } /// <summary> /// Checks if an object is null. /// </summary> /// <param name="value">A object to check.</param> /// <returns>true if object was null; otherwise, false.</returns> public static bool IsNull(this object value) { return value == null; } } /// <summary> /// Methods related to string conversion and manipulation. /// </summary> public static class StringConvert { /// <summary> /// Prefixes all line-feed characters ( (char)10 ) with a carriage return character ( (char)13 ). /// </summary> /// <param name="value">The string to convert.</param> /// <returns> /// The string with all instances of line-feed characters ( (char)10 ) prefixed with a carriage return character /// ( (char)13 ). /// </returns> public static string AddCR(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new Regex("\n(?<!\r)").Replace(value, "\r\n"); } /// <summary> /// Prefixes all carriage return characters ( (char)13 ) with a line-feed character ( (char)10 ). /// </summary> /// <param name="value">The string to convert.</param> /// <returns> /// The string with all instances of carriage return characters ( (char)13 ) prefixed with a line-feed character /// ( (char)10 ). /// </returns> public static string AddLF(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new Regex("\r(?!\n)").Replace(value, "\r\n"); } /// <summary> /// Find strings between two string delimiters. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="start">The beginning of the string to find. Passing an empty or null string starts at the beginning.</param> /// <param name="end"> /// The end of the string to find. Passing an empty or null string searches from start to end of the /// string. /// </param> /// <param name="mode">Search mode</param> /// <param name="isCaseSensitive">Is casesensitive matching</param> /// <returns>A string list collection. If no matches, then the count is zero.</returns> public static List<string> Between(this string value, string start, string end, StringBetween mode = StringBetween.None, bool isCaseSensitive = false) { end = String.IsNullOrEmpty(end) ? @"\z" : String.Format(mode == StringBetween.None ? "{0}" : "(?={0})", Regex.Escape(end)); start = String.IsNullOrEmpty(start) ? @"\A" : Regex.Escape(start); var matches = Regex.Matches(value, String.Format(@"(?{0}s){1}(.*?){2}", isCaseSensitive ? "" : "i", start, end)); var list = new List<string>(); for (int i = 0; i < matches.Count; i++) { list.Add(matches[i].Groups[1].Value); } return list; } /// <summary> /// Compact a string to a certain number of characters left and right. /// </summary> /// <param name="value">The string to convert.</param> /// <param name="countLeftAndRight">Number of characters left and right.</param> /// <returns>Compacted string; otherwise, an empty string.</returns> public static string Compact(this string value, int countLeftAndRight) { if (String.IsNullOrEmpty(value) || countLeftAndRight <= 0) { return String.Empty; } return value.Length > countLeftAndRight * 2 ? String.Format("{0}...{1}", value.Substring(0, countLeftAndRight), value.Substring(value.Length - countLeftAndRight, countLeftAndRight)) : value; } /// <summary> /// Checks if a string contains a given substring. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="search">The string to seek.</param> /// <param name="comparison">Flag related to comparison.</param> /// <param name="occurrence"> /// Which occurrence of the substring to find in the string. Use a negative occurrence to search /// from the right side. The default value is 1 (finds first occurrence). /// </param> /// <returns></returns> public static int InStr(this string value, string search, StringComparison comparison = StringComparison.OrdinalIgnoreCase, int occurrence = 1) // Based on AutoIt's StringInStr(). { const int error = -1; if (String.IsNullOrEmpty(value) || String.IsNullOrEmpty(search) || occurrence == 0) { return error; } int index = error, position = 0; if (occurrence > 0) { while (position < occurrence && (index = value.IndexOf(search, index + 1, comparison)) >= 0) { position++; } } else { occurrence = Math.Abs(occurrence); if (occurrence <= 0) { return index; } index = value.Length + 1; while (position < occurrence && (index = value.LastIndexOf(search, index - 1, comparison)) >= 0) { position++; } } return index; } /// <summary> /// Retrieve a number of characters from the left-hand side of a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="count">The number of characters to retrieve.</param> /// <returns>A string containing the leftmost count characters of the string; otherwise, an empty string.</returns> public static string Left(this string value, int count) { return String.IsNullOrEmpty(value) || count < 0 ? String.Empty : count < value.Length ? value.Substring(0, count) : value; } /// <summary> /// Convert a string to lowercase. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>String to lowercase; otherwise, an empty string.</returns> public static string Lower(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : value.ToLower(); } /// <summary> /// Extracts a number of characters from a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="start">The character position to start. ( 0 = first character ).</param> /// <param name="count">The number of characters to extract. By default the entire remainder of the string.</param> /// <returns>Extracted string.</returns> public static string Mid(this string value, int start, int? count = null) { if (String.IsNullOrEmpty(value)) { return String.Empty; } if (count.IsNull()) { count = value.Length - start; } return count < 0 || start < 0 || start + count > value.Length ? String.Empty // ReSharper disable once PossibleInvalidOperationException : value.Substring(start, (int)count); } /// <summary> /// Convert a null string to an empty string. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>An empty string; otherwise, the original string.</returns> public static string NullToEmpty(this string value) { return value.IsNull() ? String.Empty : value; } /// <summary> /// Splits a path into the drive, directory, file name and file extension parts. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>A string array with 0 = filepath, 1 = drive, 2 = directory, 3 = filename, 4 = extension.</returns> public static string[] PathSplit(this string value) { const int eFilePath = 0, eDrive = 1, eDir = 2, eFileName = 3, eExtension = 4, eMax = 5; var pathSplit = new string[eMax]; pathSplit[eFilePath] = value; if (String.IsNullOrWhiteSpace(value)) { return pathSplit; } int extension = value.LastIndexOf(".", StringComparison.OrdinalIgnoreCase), slash; if (value.IndexOf("/", StringComparison.OrdinalIgnoreCase) >= 0) { value = value.Replace("/", @"\"); // Replace '/' with '\' } pathSplit[eDir] = value; pathSplit[eDrive] = value.Substring(0, 2); // Drive. if (pathSplit[eDrive] == @"\\") // UNC path. { slash = -1; int position = 1; while (position <= 3 && (slash = value.IndexOf(@"\", slash + 1, StringComparison.OrdinalIgnoreCase)) >= 0) { position++; } if (slash >= 0) { pathSplit[eDrive] = value.Substring(0, slash - 1); } pathSplit[eDir] = "A:" + pathSplit[eDir].Substring(pathSplit[eDrive].Length); // A: is appended to mimic the standard value if it wasn't UNC. extension = value.LastIndexOf(".", StringComparison.OrdinalIgnoreCase); } slash = pathSplit[eDir].LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase); if (extension >= 0) // If an extension exists. { pathSplit[eExtension] = pathSplit[eDir].Substring(extension); // Extension. pathSplit[eFileName] = pathSplit[eDir].Substring(slash); pathSplit[eFileName] = pathSplit[eFileName].Substring(1, pathSplit[eFileName].Length - pathSplit[eExtension].Length - 1); } else { if (pathSplit[eDir][pathSplit[eDir].Length - 1] != '\\') // If backslash doesn't exist (when it's a directory) then append to the end. { pathSplit[eDir] += @"\"; slash += 1; // slash = pathSplit[eDir].LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase); } } pathSplit[eDir] = pathSplit[eDir].Substring(0, slash + 1); // Path. pathSplit[eDir] = pathSplit[eDir].Substring(2); return pathSplit; } /// <summary> /// Repeats a string a specified number of times. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="count">Number of times to repeat the string.</param> /// <returns>Repeated string; otherwise, an empty string.</returns> public static string Repeat(this string value, int count) { if (String.IsNullOrEmpty(value) || count <= 0) { return String.Empty; } string result = String.Empty; // From AutoIt's _StringRepeat(). while (count > 1) { if ((count & 1) == 1) { result += value; } value += value; count = count >> 1; } return value + result; //return String.Join(value, new string[++count]); // http://rosettacode.org/wiki/Repeat_a_string#C.23 //var @string = new StringBuilder(value.Length * count--); //@string.Append(value); //for (int i = 0; i < count; i++) //{ // @string.Append(value); //} //return @string.ToString(); } /// <summary> /// Reverses the contents of a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <returns>Reversed string; otherwise, an empty string.</returns> public static string Reverse(this string value) { if (String.IsNullOrEmpty(value)) { return String.Empty; } var array = value.ToCharArray(); for (int i = 0, j = array.Length - 1; i < j; i++, j--) // This version is a slightly optimised version than Array.Reverse(). { char temp = array[i]; array[i] = array[j]; array[j] = temp; } return new string(array); //char[] array = value.ToCharArray(); //Array.Reverse(array); //return new string(array); } /// <summary> /// Retrieve a number of characters from the right-hand side of a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="count">The number of characters to retrieve.</param> /// <returns>A string containing the rightmost count characters of the string; otherwise, an empty string.</returns> public static string Right(this string value, int count) { return String.IsNullOrEmpty(value) || count < 0 ? String.Empty : count < value.Length ? value.Substring(value.Length - count, count) : value; } /// <summary> /// Convert the string to Rot13 (alpha characters only). /// </summary> /// <param name="value">The string to convert.</param> /// <returns>Rot13 string; otherwise, an empty string.</returns> public static string Rot13(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new string(value.ToCharArray().Select( @char => (char) (@char >= 'A' && @char <= 'M' || @char >= 'a' && @char <= 'm' ? @char + 13 : @char >= 'N' && @char <= 'Z' || @char >= 'n' && @char <= 'z' ? @char - 13 : @char ) ).ToArray()); //if (String.IsNullOrEmpty(value)) //{ // return String.Empty; //} //char[] array = value.ToCharArray(); //for (int i = 0; i < array.Length; i++) //{ // if (array[i] >= 'A' && array[i] <= 'M' || array[i] >= 'a' && array[i] <= 'm') // { // array[i] = (char)(array[i] + 13); // } // else if (array[i] >= 'N' && array[i] <= 'Z' || array[i] >= 'n' && array[i] <= 'z') // { // array[i] = (char)(array[i] - 13); // } //} //return new string(array); } /// <summary> /// Convert the string to Rot18 (alphanumeric only). /// </summary> /// <param name="value">The string to convert.</param> /// <returns>Rot18 string; otherwise, an empty string.</returns> public static string Rot18(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new string(value.ToCharArray().Select( @char => (char) ( @char >= '0' && @char <= '4' ? @char + 5 : @char >= '5' && @char <= '9' ? @char - 5 : @char >= 'A' && @char <= 'M' || @char >= 'a' && @char <= 'm' ? @char + 13 : @char >= 'N' && @char <= 'Z' || @char >= 'n' && @char <= 'z' ? @char - 13 : @char ) ).ToArray()); } /// <summary> /// Convert the string to Rot47 (alphanumeric and symbols only). /// </summary> /// <param name="value">The string to convert.</param> /// <returns>Rot47 string; otherwise, an empty string.</returns> public static string Rot47(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new string(value.ToCharArray().Select( @char => (char) ( @char >= 33 && @char <= 79 ? @char + 47 : @char >= 80 && @char <= 126 ? @char - 47 : @char ) ).ToArray()); } /// <summary> /// Convert the string to Rot5 (digits only). /// </summary> /// <param name="value">The string to convert.</param> /// <returns>Rot5 string; otherwise, an empty string.</returns> public static string Rot5(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new string(value.ToCharArray().Select( @char => (char) ( !Char.IsDigit(@char) ? @char : @char >= '0' && @char <= '4' ? @char + 5 : @char - 5 ) ).ToArray()); } /// <summary> /// Removes all carriage return values ( (char)13 ) from a string. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>The string with all instances of the (char)13 character removed; otherwise, an empty string.</returns> public static string StripCR(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new Regex("\r").Replace(value, String.Empty); } /// <summary> /// Removes all line-feed values ( (char)10 ) from a string. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>The string with all instances of the (char)10 character removed; otherwise, an empty string.</returns> public static string StripLF(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : new Regex("\n").Replace(value, String.Empty); } /// <summary> /// Strips the white space ( (char)0, (char)9-(char)13 and (char)32 ) in a string. /// </summary> /// <param name="value">The string to convert.</param> /// <param name="flags">Flags related to stripping white space.</param> /// <returns>The new string stripped of the requested white space; otherwise, an empty string.</returns> public static string StripWS(this string value, StringStripWS flags) { if (String.IsNullOrEmpty(value)) { return String.Empty; } if ((flags & StringStripWS.All) == StringStripWS.All) { return new Regex(@"\s").Replace(value, String.Empty); } if ((flags & StringStripWS.Spaces) == StringStripWS.Spaces) { value = Regex.Replace(value, @"\s{2,}", String.Empty); } if ((flags & StringStripWS.Leading) == StringStripWS.Leading) { value = value.TrimStart(); } if ((flags & StringStripWS.Trailing) == StringStripWS.Trailing) { value = value.TrimEnd(); } return value; } /// <summary> /// Trims a number of characters from the left hand side of a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="count">The number of characters to trim.</param> /// <returns>The string trimmed by count characters from the left; otherwise, an empty string.</returns> public static string TrimLeft(this string value, int count) { if (String.IsNullOrEmpty(value)) { return String.Empty; } count = (count < 0 || count >= value.Length ? value.Length : count); return value.Substring(count, value.Length - count); } /// <summary> /// Trims a number of characters from the right hand side of a string. /// </summary> /// <param name="value">The string to evaluate.</param> /// <param name="count">The number of characters to trim.</param> /// <returns>The string trimmed by count characters from the right; otherwise, an empty string.</returns> public static string TrimRight(this string value, int count) { if (String.IsNullOrEmpty(value)) { return String.Empty; } count = value.Length - (count < 0 || count > value.Length ? value.Length : count); return value.Substring(0, count); } /// <summary> /// Convert a string to uppercase. /// </summary> /// <param name="value">The string to convert.</param> /// <returns>String to uppercase; otherwise, an empty string.</returns> public static string Upper(this string value) { return String.IsNullOrEmpty(value) ? String.Empty : value.ToUpper(); } } /// <summary> /// Extension methods related to checking if a string is equal to a condition. /// </summary> public static class StringIs { /// <summary> /// Checks if a string is representing T. /// </summary> /// <typeparam name="T">Specifies the type to check.</typeparam> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was a T datatype; otherwise, false.</returns> public static bool Is<T>(this string value) // http://stackoverflow.com/questions/1654871/generic-tryparse-extension-method { var type = typeof(T); var tryParseMethod = type.GetMethod( "TryParse", new[] { typeof (string), type.MakeByRefType() }); if (tryParseMethod.IsNull()) { return false; } bool result = false; object[] parameters = { value, default(T) }; var isExecuted = tryParseMethod.Invoke(null, parameters); if (isExecuted is bool) { result = (bool)isExecuted; } return result; } /// <summary> /// Checks if a string contains only alphanumeric characters ( 0-9 and A-Z ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only alphanumeric characters; otherwise, false.</returns> public static bool IsAlNum(this string value) { return !String.IsNullOrEmpty(value) && value.All(Char.IsLetterOrDigit); } /// <summary> /// Checks if a string contains only alphabetic characters ( A-Z ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only alphabetic characters; otherwise, false.</returns> public static bool IsAlpha(this string value) { return !String.IsNullOrEmpty(value) && value.All(Char.IsLetter); } /// <summary> /// Checks if a string contains only ASCII characters ( 0-127 ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only ASCII characters; otherwise, false.</returns> public static bool IsASCII(this string value) { return !String.IsNullOrEmpty(value) && value.All(@char => @char <= '\x007f'); } /// <summary> /// Checks if a string is representing a boolean value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsBool(this string value) { bool result; return bool.TryParse(value, out result); } /// <summary> /// Checks if a string is representing an 8-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsByte(this string value) { byte result; return byte.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a char value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsChar(this string value) { char result; return char.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a DateTime value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsDateTime(this string value) { DateTime result; return DateTime.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a decimal value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsDecimal(this string value) { decimal result; return decimal.TryParse(value, out result); } /// <summary> /// Checks if a string contains only digit characters ( 0-9 ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if value contains only digit characters; otherwise, false.</returns> public static bool IsDigit(this string value) { return !String.IsNullOrEmpty(value) && new Regex(@"^[-+]?\d+$", RegexOptions.Compiled).IsMatch(value); } /// <summary> /// Checks if a string is representing a double value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsDouble(this string value) { double result; return double.TryParse(value, out result); } /// <summary> /// Checks if a string is representing an email. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string was an email; otherwise, false.</returns> public static bool IsEmail(this string value) { return !String.IsNullOrEmpty(value) && new Regex( @"^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$") .IsMatch(value); // http://www.regular-expressions.info/email.html, modified by guinness. } /// <summary> /// Check if a string is empty. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string was empty; otherwise, false.</returns> public static bool IsEmpty(this string value) { return value.IsNotNull() && value.Length == 0; } /// <summary> /// Checks if a string is representing a float value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsFloat(this string value) { float result; return float.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a Guid value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsGuid(this string value) { return !String.IsNullOrEmpty(value) && new Regex(@"^(?:(?<curly>\{)?[0-9A-Fa-f]{8}-(?:[0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}(?(curly)\}))$") .IsMatch(value); //Guid result; //return Guid.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a hexadecimal value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsHex(this string value) { return !String.IsNullOrEmpty(value) && new Regex(@"^0[xX][\dA-Fa-f]+$").IsMatch(value); } /// <summary> /// Checks if a string is representing a 32-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsInt(this string value) { int result; return int.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a 16-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsInt16(this string value) { return IsShort(value); } /// <summary> /// Checks if a string is representing a 32-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsInt32(this string value) { return IsInt(value); } /// <summary> /// Checks if a string is representing a 64-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsInt64(this string value) { return IsLong(value); } /// <summary> /// Checks if a string is representing an IP address. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was an IP address; otherwise, false.</returns> public static bool IsIPAddress(this string value) { // Regular expression modified by guinness. return !String.IsNullOrEmpty(value) && new Regex(value).IsMatch( @"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); } /// <summary> /// Checks if a string contains a valid ISBN number. /// </summary> /// <param name="isbn">A string containing the value to check.</param> /// <param name="type">An ISBN flag of the type to check.</param> /// <returns>true if a valid ISBN number; otherwise, false.</returns> public static bool IsISBN(string isbn, ISBN type) { if (String.IsNullOrWhiteSpace(isbn)) { return false; } int length = isbn.Length - 1; int counter, sum = 0; switch (type) { case ISBN.Ten: { if (isbn[length] == 'x' || isbn[length] == 'X') { length -= 1; sum = 10; } counter = 10; for (int i = 0; i <= length; i++) { if (isbn[i] < '0' || isbn[i] > '9') { continue; } sum += (isbn[i] - '0') * counter; counter -= 1; } return sum % 11 == 0; // Divisible by 11. } case ISBN.Thirteen: { const int one = 1, three = 3; counter = one; for (int i = 0; i <= length; i++) { if (isbn[i] < '0' || isbn[i] > '9') { continue; } sum += (isbn[i] - '0') * counter; counter = (counter == one) ? three : one; } return sum % 10 == 0; // Divisible by 10. } } return false; } /// <summary> /// Checks if a string is representing a 64-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsLong(this string value) { long result; return long.TryParse(value, out result); } /// <summary> /// Checks if a string contains only lowercase characters. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only lowercase characters.; otherwise, false.</returns> public static bool IsLower(this string value) { return !String.IsNullOrEmpty(value) && value.All(Char.IsLower); } /// <summary> /// Check if a string is null or empty. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string was null or empty; otherwise, false.</returns> public static bool IsNullOrEmpty(this string value) { return String.IsNullOrEmpty(value); } /// <summary> /// Checks if a string is representing a numerical representation. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if string was a numerical representation; otherwise, false.</returns> public static bool IsNumerical(this string value) { return !String.IsNullOrEmpty(value) && new Regex( String.Format(@"^[-+]?\d+(?:{0}\d+)?$", NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), RegexOptions.Compiled).IsMatch(value); //return IsDouble(value); } /// <summary> /// Checks if a string is a palindrome. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string was a palindrome; otherwise, false.</returns> public static bool IsPalindrome(this string value) { if (String.IsNullOrEmpty(value)) { return false; } for (int i = 0, j = value.Length - 1; i < j; i++, j--) { while (!Char.IsLetterOrDigit(value[i])) { i++; } while (!Char.IsLetterOrDigit(value[j])) { j--; } if (!Char.ToUpper(value[i]).Equals(Char.ToUpper(value[j]))) { return false; } } return true; //value = Regex.Replace(value, @"\W", ""); // Remove all non-word characters. //char[] array = value.ToCharArray(); //Array.Reverse(array); //return String.Equals(new string(array), value, StringComparison.CurrentCultureIgnoreCase); } /// <summary> /// Checks if a string is representing an 8-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsSByte(this string value) { sbyte result; return sbyte.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a 16-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsShort(this string value) { short result; return short.TryParse(value, out result); } /// <summary> /// Checks if a string contains only whitespace characters ( (char)0, (char)9-(char)13 and (char)32 ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only whitespace characters; otherwise, false.</returns> public static bool IsSpace(this string value) { return String.IsNullOrEmpty(value) || value.All(Char.IsWhiteSpace); //return !String.IsNullOrEmpty(value) && value.All(@char => (@char >= '\x0009' && @char <= '\x000d') || @char == '\x0000' || @char == '\x0020'); } /// <summary> /// Checks if a string contains characters. /// </summary> /// <param name="value">A string containing characters to check.</param> /// <returns>true if string contained characters; otherwise, false.</returns> public static bool IsString(this string value) { return !String.IsNullOrEmpty(value); } /// <summary> /// Determines the TypeCode the string is representing. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>TypeCode the string is representing.</returns> public static TypeCode IsType(this string value) { if (String.IsNullOrEmpty(value)) { return TypeCode.Empty; } if (new Regex(@"^-?\d+$").IsMatch(value)) // Integer { if (value.Substring(0, 1) == "-") // Negative number. { SByte @sbyte; if (SByte.TryParse(value, out @sbyte)) { return TypeCode.SByte; } Int16 int16; if (Int16.TryParse(value, out int16)) { return TypeCode.Int16; } Int32 int32; if (Int32.TryParse(value, out int32)) { return TypeCode.Int32; } Int64 int64; if (Int64.TryParse(value, out int64)) { return TypeCode.Int64; } } else { Byte @byte; if (Byte.TryParse(value, out @byte)) { return TypeCode.Byte; } UInt16 uint16; if (UInt16.TryParse(value, out uint16)) { return TypeCode.UInt16; } UInt32 uint32; if (UInt32.TryParse(value, out uint32)) { return TypeCode.Int32; } UInt64 uint64; if (UInt64.TryParse(value, out uint64)) { return TypeCode.UInt64; } } } else // Double { string formatted = String.Format(@"^[\d{0}]+$", NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); if (new Regex(formatted).IsMatch(value)) { Single @float; if (Single.TryParse(value, out @float)) { return TypeCode.Single; } Double @double; if (Double.TryParse(value, out @double)) { return TypeCode.Double; } Decimal @decimal; if (Decimal.TryParse(value, out @decimal)) { return TypeCode.Decimal; } } else { Boolean @bool; if (Boolean.TryParse(value, out @bool)) { return TypeCode.Boolean; } Char @char; if (Char.TryParse(value, out @char)) { return TypeCode.Char; } DateTime datetime; if (DateTime.TryParse(value, out datetime)) { return TypeCode.DateTime; } } } return TypeCode.String; } /// <summary> /// Checks if a string is representing a 32-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsUInt(this string value) { uint result; return uint.TryParse(value, out result); } /// <summary> /// Checks if a string is representing a 16-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsUInt16(this string value) { return IsUShort(value); } /// <summary> /// Checks if a string is representing a 32-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsUInt32(this string value) { return IsUInt(value); } /// <summary> /// Checks if a string is representing a 64-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsUInt64(this string value) { return IsULong(value); } /// <summary> /// Checks if a string is representing a 64-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsULong(this string value) { ulong result; return ulong.TryParse(value, out result); } /// <summary> /// Checks if a string contains only uppercase characters. /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains contains only uppercase characters; otherwise, false.</returns> public static bool IsUpper(this string value) { return !String.IsNullOrEmpty(value) && value.All(Char.IsUpper); } /// <summary> /// Checks if a string is representing a 16-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was the correct datatype; otherwise, false.</returns> public static bool IsUShort(this string value) { ushort result; return ushort.TryParse(value, out result); } /// <summary> /// Checks if a string doesn't contain a set of characters. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <param name="invalid">A pattern of characters that shouldn't be present e.g. '\/|:"'</param> /// <returns>true if string didn't contain the set of characters; otherwise, false.</returns> public static bool IsValid(this string value, string invalid) { return !String.IsNullOrEmpty(value) && !String.IsNullOrEmpty(invalid) && new Regex(String.Format(@"[{0}]", Regex.Escape(invalid)), RegexOptions.IgnoreCase).IsMatch(value); } /// <summary> /// Checks if a string doesn't contain a set of characters. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <param name="invalid">A char array of characters that shouldn't be present e.g. '\/|:"'</param> /// <returns>true if string didn't contain the set of characters; otherwise, false.</returns> public static bool IsValid(this string value, char[] invalid) { return !invalid.IsNull() && invalid.Length != 0 && value.IsValid(new string(invalid)); } /// <summary> /// Checks if a filepath contains an approved filetype extension. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <param name="fileTypes">A string of filetype extensions delimited with ';' e.g. 'exe;txt;bat'.</param> /// <returns>true if filepath was approved; otherwise, false.</returns> public static bool IsValidType(this string value, string fileTypes) { return !String.IsNullOrEmpty(value) && !String.IsNullOrEmpty(fileTypes) && new Regex(String.Format(@"\.(?:{0})$", Regex.Escape(fileTypes).Replace(';', '|')), RegexOptions.IgnoreCase).IsMatch(value); } /// <summary> /// Checks if a string is representing a web safe color. /// </summary> /// <param name="value">A string containing the value to check.</param> /// <returns>true if value was a web safe color; otherwise, false.</returns> public static bool IsWebSafeColor(this string value) { return !String.IsNullOrEmpty(value) && new Regex(@"^(?:#|0[xX])?(([CcFf0369])\2){3}$").IsMatch(value); } /// <summary> /// Checks if a string contains only hexadecimal digit characters ( 0-9 and A-F ). /// </summary> /// <param name="value">A string to check.</param> /// <returns>true if string contains only hexadecimal digit characters; otherwise, false.</returns> public static bool IsXDigit(this string value) { return !String.IsNullOrEmpty(value) && value.All(@char => Char.IsDigit(@char) || (@char >= 'a' && @char <= 'f') || (@char >= 'A' && @char <= 'F')); //return !String.IsNullOrEmpty(value) && new Regex(@"^[\dA-Fa-f]+$", RegexOptions.Compiled).IsMatch(value); } } /// <summary> /// Extension methods related to parsing strings to another datatype. /// </summary> public static class StringParse { /// <summary> /// Convert a string as a T value. /// </summary> /// <typeparam name="T">Specifies the type to convert to.</typeparam> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a T datatype.</returns> public static T ParseAs<T>(this string value) // http://stackoverflow.com/questions/1654871/generic-tryparse-extension-method { var type = typeof(T); var tryParseMethod = type.GetMethod( "TryParse", new[] { typeof (string), type.MakeByRefType() }); var result = default(T); if (tryParseMethod.IsNull()) { return result; } object[] parameters = { value, result }; var isExecuted = tryParseMethod.Invoke(null, parameters); if (!(isExecuted is bool)) { return result; } bool successful = (bool)isExecuted; if (successful) { result = (T)parameters[1]; } return result; } /// <summary> /// Convert a string as a boolean value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a boolean datatype.</returns> public static bool ParseAsBool(this string value) { bool result; bool.TryParse(value, out result); return result; } /// <summary> /// Convert a string as an 8-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of the 8-bit unsigned integer datatype.</returns> public static byte ParseAsByte(this string value) { byte result; byte.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a char value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a char datatype.</returns> public static char ParseAsChar(this string value) { char result; char.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a DateTime value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a DateTime datatype.</returns> public static DateTime ParseAsDateTime(this string value) { DateTime result; DateTime.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a decimal value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a decimal datatype.</returns> public static decimal ParseAsDecimal(this string value) { decimal result; decimal.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a double value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a double datatype.</returns> public static double ParseAsDouble(this string value) { double result; double.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a float value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a float datatype.</returns> public static float ParseAsFloat(this string value) { float result; float.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a hexadecimal number to an unsigned 32-bit integer. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 32-bit unsigned integer datatype.</returns> public static uint ParseAsHex(this string value) { uint result = default(uint); if (!value.IsHex()) { return result; } value = value.Substring(2); uint.TryParse(value, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out result); return result; } /// <summary> /// Convert a string as a 32-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 32-bit signed integer datatype.</returns> public static int ParseAsInt(this string value) { int result; int.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a 16-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 16-bit signed integer datatype.</returns> public static Int16 ParseAsInt16(this string value) { return ParseAsShort(value); } /// <summary> /// Convert a string as a 32-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 32-bit signed integer datatype.</returns> public static Int32 ParseAsInt32(this string value) { return ParseAsInt(value); } /// <summary> /// Convert a string as a 64-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 64-bit signed integer datatype.</returns> public static Int64 ParseAsInt64(this string value) { return ParseAsLong(value); } /// <summary> /// Convert a string as a 64-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 64-bit signed integer datatype.</returns> public static long ParseAsLong(this string value) { long result; long.TryParse(value, out result); return result; } /// <summary> /// Convert a string as an 8-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 8-bit signed integer datatype.</returns> public static sbyte ParseAsSByte(this string value) { sbyte result; sbyte.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a 16-bit signed integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 16-bit signed integer datatype.</returns> public static short ParseAsShort(this string value) { short result; short.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a 32-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 32-bit unsigned integer datatype.</returns> public static uint ParseAsUInt(this string value) { uint result; uint.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a 16-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 16-bit unsigned integer datatype.</returns> public static UInt16 ParseAsUInt16(this string value) { return ParseAsUShort(value); } /// <summary> /// Convert a string as a 32-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 32-bit unsigned integer datatype.</returns> public static UInt32 ParseAsUInt32(this string value) { return ParseAsUInt(value); } /// <summary> /// Convert a string as a 64-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 64-bit unsigned integer datatype.</returns> public static UInt64 ParseAsUInt64(this string value) { return ParseAsULong(value); } /// <summary> /// Convert a string as a 64-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 64-bit unsigned integer datatype.</returns> public static ulong ParseAsULong(this string value) { ulong result; ulong.TryParse(value, out result); return result; } /// <summary> /// Convert a string as a 16-bit unsigned integer value. /// </summary> /// <param name="value">A string containing the value to convert.</param> /// <returns>converted value; otherwise, default value of a 16-bit unsigned integer datatype.</returns> public static ushort ParseAsUShort(this string value) { ushort result; ushort.TryParse(value, out result); return result; } } } Edited January 11, 2015 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 9, 2015 Author Share Posted January 9, 2015 Added Rot5, Rot13, Rot18 and Rot47 extension methods to post #9. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
iMac0 Posted January 10, 2015 Share Posted January 10, 2015 Interessante, porem não sei como ajudar ... Link to comment Share on other sites More sharing options...
guinness Posted January 11, 2015 Author Share Posted January 11, 2015 Added Between, InStr and PathSplit extension methods to post #9. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 11, 2015 Author Share Posted January 11, 2015 Added IsISBN extension method to post #9. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now