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.
59 lines
1.9 KiB
59 lines
1.9 KiB
using System.Globalization; |
|
using Newtonsoft.Json.Linq; |
|
|
|
namespace BrilliantSightClient.Model.Extension; |
|
|
|
public static class StringExtension |
|
{ |
|
/// <summary> |
|
/// 简单的判断字符串是否为Null或空白 |
|
/// </summary> |
|
/// <param name="str"></param> |
|
/// <returns></returns> |
|
public static bool IsNullOrEmpty(this string str) |
|
{ |
|
return String.IsNullOrEmpty(str); |
|
} |
|
/// <summary> |
|
/// 字符串按类型转为JToken |
|
/// </summary> |
|
/// <param name="value"></param> |
|
/// <returns></returns> |
|
public static JToken ConvertJTokenValue(this string value) |
|
{ |
|
// 处理布尔类型(不区分大小写) |
|
if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)) |
|
return new JValue(true); |
|
if (string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)) |
|
return new JValue(false); |
|
|
|
// 处理数值类型 |
|
if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double numericValue)) |
|
{ |
|
// 判断是否为整数 |
|
if (numericValue == Math.Truncate(numericValue)) |
|
{ |
|
// 在int范围内转换为整型 |
|
if (numericValue >= int.MinValue && numericValue <= int.MaxValue) |
|
return new JValue((int)numericValue); |
|
|
|
// 超出int范围使用long类型 |
|
return new JValue((long)numericValue); |
|
} |
|
|
|
// 保留小数部分 |
|
return new JValue(numericValue); |
|
} |
|
|
|
// 默认处理为字符串 |
|
return new JValue(value); |
|
} |
|
|
|
public static double ToDouble(this string str) |
|
{ |
|
if(!string.IsNullOrWhiteSpace(str)) |
|
if (double.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out double value)) |
|
return value; |
|
return 0; |
|
} |
|
} |