1. 程式人生 > >【C#】字串格式化字元型、日期型、布林型

【C#】字串格式化字元型、日期型、布林型

 #region 格式化字元型、日期型、布林型 /StringFormat(string str, Type type)
        /// <summary>
        /// 格式化字元型、日期型、布林型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                //str = "\"" + str + "\"";
                if (string.IsNullOrEmpty(str.Trim()))
                {
                    str = "\"" + str + "\"";
                }
                else
                {
                    str = "\"" + DateTime.Parse(str.Trim()).ToString("yyyy-MM-dd HH:mm:ss") + "\"";
                }
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            else if (type != typeof(string) && string.IsNullOrEmpty(str))
            {
                str = "\"" + str + "\"";
            }
            return str;
        }
        
        #endregion