You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.7 KiB
108 lines
3.7 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
|
|
public static class StringExtensions
|
|
{
|
|
/// <summary>
|
|
/// 将string转换为DateTime,若转换失败,则返回默认值。
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static DateTime? ToDateTime(this string str, DateTime? defaultValue = null)
|
|
{
|
|
DateTime val;
|
|
if (string.IsNullOrWhiteSpace(str))
|
|
{
|
|
return defaultValue.GetValueOrDefault();
|
|
}
|
|
if (str.Contains("-") || str.Contains("/"))
|
|
{
|
|
if (DateTime.TryParse(str, out val))
|
|
return val;
|
|
|
|
return defaultValue.GetValueOrDefault();
|
|
}
|
|
else
|
|
{
|
|
int length = str.Length;
|
|
switch (length)
|
|
{
|
|
case 4:
|
|
if (DateTime.TryParseExact(str, "yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
case 6:
|
|
if (DateTime.TryParseExact(str, "yyyyMM", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
case 8:
|
|
if (DateTime.TryParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
case 10:
|
|
if (DateTime.TryParseExact(str, "yyyyMMddHH", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
case 12:
|
|
if (DateTime.TryParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
case 14:
|
|
if (DateTime.TryParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
default:
|
|
if (DateTime.TryParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None, out val))
|
|
return val;
|
|
return defaultValue.GetValueOrDefault();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 强制转换类型
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
|
|
{
|
|
foreach (object item in source)
|
|
{
|
|
yield return (TResult)Convert.ChangeType(item, typeof(TResult));
|
|
}
|
|
}
|
|
|
|
// 转换为long
|
|
public static long ToLong(this string str, long defaultValue = 0L) =>
|
|
long.TryParse(str, out var result) ? result : defaultValue;
|
|
|
|
// 转换为int
|
|
public static int ToInt(this string str, int defaultValue = 0) =>
|
|
int.TryParse(str, out var result) ? result : defaultValue;
|
|
|
|
// 转换为short
|
|
public static short ToShort(this string str, short defaultValue = 0) =>
|
|
short.TryParse(str, out var result) ? result : defaultValue;
|
|
|
|
// 转换为decimal
|
|
public static decimal ToDecimal(this string str, decimal defaultValue = 0m) =>
|
|
decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var result) ? result : defaultValue;
|
|
|
|
// 转换为bool
|
|
public static bool ToBool(this string str, bool defaultValue = false) =>
|
|
bool.TryParse(str, out var result) ? result : defaultValue;
|
|
|
|
// 转换为float
|
|
public static float ToFloat(this string str, float defaultValue = 0f) =>
|
|
float.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var result) ? result : defaultValue;
|
|
|
|
// 转换为double
|
|
public static double ToDouble(this string str, double defaultValue = 0d) =>
|
|
double.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var result) ? result : defaultValue;
|
|
|
|
// 转换为Guid
|
|
public static Guid ToGuid(this string str, Guid defaultValue = default) =>
|
|
Guid.TryParse(str, out var guid) ? guid : defaultValue;
|
|
|
|
}
|