C # general methods for determining types and converting types,

Source: Internet
Author: User

C # general methods for determining types and converting types,

In the process of code writing, the use and definition of basic data types are involved. In the project, it is sometimes troublesome to determine and convert types, first, we provide several methods to determine the basic data types:

1. Determine whether the object is an Int32 Number:

/// <Summary> /// determines whether the object is an Int32 number. /// </summary> /// <param name = "expression"> </param>/ // <returns> </returns> public static bool IsNumeric (object expression) {return expression! = Null & IsNumeric (expression. toString ());} /// <summary> /// determines whether the object is an Int32 number. /// </summary> /// <param name = "expression"> </param>/ // <returns> </returns> public static bool IsNumeric (string expression) {var str = expression; if (! (Str. Length> 0) | str. Length> 11 |! System. Text. RegularExpressions. Regex. IsMatch (str, @ "^ [-]? [0-9] * [.]? [0-9] * $ ") return false; return (str. length <10) | (str. length = 10 & str [0] = '1') | (str. length = 11 & str [0] = '-' & str [1] = '1 ');}

2. Whether it is Double type:

/// <Summary> /// whether it is of the Double type /// </summary> /// <param name = "expression"> </param> /// <returns> </returns> public static bool IsDouble (object expression) {if (expression! = Null) return System. text. regularExpressions. regex. isMatch (expression. toString (), @ "^ ([0-9]) [0-9] * (\. \ w *)? $ "); Return false ;}

3. convert a string to an array:

/// <Summary> /// convert the string to an array /// </summary> /// <param name = "str"> string </param> /// <returns> string Array </returns> public static string [] GetStrArray (string str) {return str. split (new char [',']);}

4. Convert the array to a string:

/// <Summary> /// convert the array to a string /// </summary> /// <param name = "list"> List </param> /// <param name = "speater"> delimiter </param> // <returns> String </returns> public static string GetArrayStr (List <string> list, string speater) {var sb = new StringBuilder (); for (int I = 0; I <list. count; I ++) {if (I = list. count-1) {sb. append (list [I]);} else {sb. append (list [I]); sb. append (speater) ;}} return sb. toString ();}

5. Convert the object type to the bool type:

/// <Summary> // convert the object type to bool type /// </summary> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> // <returns> converted bool type result </returns> public static bool StrToBool (object expression, bool defValue) {if (expression! = Null) return StrToBool (expression, defValue); return defValue ;}

6. Convert string type to bool type:

/// <Summary> // convert string type to bool type /// </summary> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> // <returns> converted bool type result </returns> public static bool StrToBool (string expression, bool defValue) {if (expression = null) return defValue; if (string. compare (expression, "true", StringComparison. ordinalIgnoreCase) = 0) return true; return string. compare (expression, "fals E ", StringComparison. OrdinalIgnoreCase )! = 0 & defValue ;}

7. Convert the object to Int32:

/// <Summary> /// convert the object to the Int32 type /// </summary> /// <param name = "expression"> string to be converted </param >/// <param name = "defValue"> default value </param> /// <returns> converted int type result </returns> public static int ObjToInt (object expression, int defValue) {return expression! = Null? StrToInt (expression. ToString (), defValue): defValue ;}

8. Convert string to Int32 type:

/// <Summary> /// convert the string to the Int32 type /// </summary> /// <param name = "expression"> string to be converted </param >/// <param name = "defValue"> default value </param> /// <returns> converted int type result </returns> public static int StrToInt (string expression, int defValue) {if (string. isNullOrEmpty (expression) | expression. trim (). length> = 11 |! System. text. regularExpressions. regex. isMatch (expression. trim (), @ "^ ([-] | [0-9]) [0-9] * (\. \ w *)? $ ") Return defValue; int rv; if (int. TryParse (expression, out rv) return rv; return Convert. ToInt32 (StrToFloat (expression, defValue ));}

9. Convert the Object type to the decimal type:

/// <Summary> // convert the Object type to decimal type // </summary> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> // <returns> converted decimal type result </returns> public static decimal ObjToDecimal (object expression, decimal defValue) {if (expression! = Null) return StrToDecimal (expression. ToString (), defValue); return defValue ;}

10. Convert string type to decimal type:

/// <Summary> // convert string type to decimal type /// </summary> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> // <returns> converted decimal type result </returns> public static decimal StrToDecimal (string expression, decimal defValue) {if (expression = null) | (expression. length> 10) return defValue; decimal intValue = defValue; {bool isDecimal = System. text. regularExpressions. regex. isM Atch (expression, @ "^ ([-] | [0-9]) [0-9] * (\. \ w *)? $ "); If (isDecimal) decimal. TryParse (expression, out intValue);} return intValue ;}

11. Convert the Object type to the float type:

/// <Summary> // convert Object type to float type /// </summary> /// <param name = "strValue"> string to be converted </param> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> /// <returns> after conversion result of the int type </returns> public static float ObjToFloat (object expression, float defValue) {if (expression! = Null) return StrToFloat (expression. ToString (), defValue); return defValue ;}

12. Convert string type to float type:

/// <Summary> // string type to float type /// </summary> /// <param name = "expression"> </param> /// <param name = "defValue"> default value </param> // <returns> converted int type result </returns> public static float StrToFloat (string expression, float defValue) {if (expression = null) | (expression. length> 10) return defValue; float intValue = defValue; {bool isFloat = System. text. regularExpressions. regex. isMatch (expression, @ "^ ([-] | [0-9]) [0-9] * (\. \ w *)? $ "); If (isFloat) float. TryParse (expression, out intValue);} return intValue ;}

13. convert an object to a date-time type (reload of several operation methods is provided ):

/// <Summary> /// convert the object to the datetime type /// </summary> /// <param name = "str"> string to be converted </ param> /// <param name = "defValue"> default value </param> /// <returns> converted int type result </returns> public static DateTime StrToDateTime (string str, dateTime defValue) {if (! String. isNullOrEmpty (str) {DateTime dateTime; if (DateTime. tryParse (str, out dateTime) return dateTime;} return defValue ;} /// <summary> /// convert the object to the datetime type /// </summary> /// <param name = "str"> string to be converted </ param> // <returns> converted int type result </returns> public static DateTime StrToDateTime (string str) {return StrToDateTime (str, DateTime. now );} /// <summary> /// convert the object to the datetime type /// </summary> /// <param name = "obj"> object to be converted </ param> // <returns> converted int type result </returns> public static DateTime ObjectToDateTime (object obj) {return StrToDateTime (obj. toString ());} /// <summary> /// convert the object to the datetime type /// </summary> /// <param name = "obj"> object to be converted </ param> /// <param name = "defValue"> default value </param> /// <returns> converted int type result </returns> public static DateTime ObjectToDateTime (object obj, dateTime defValue) {return StrToDateTime (obj. toString (), defValue );}

14. convert an object to a string:

/// <Summary> /// convert the object to a string /// </summary> /// <param name = "obj"> object to be converted </param> /// <returns> converted string type result </returns> public static string ObjectToStr (object obj) {if (obj = null) return ""; return obj. toString (). trim ();}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.