namespace YLErp.Helpers
{
public static class StringHelper
{
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static string GetFixedStr(string desc1, string desc2, int total1 = 40, int total2 = 20)
{
int remainLength = total1 - System.Text.Encoding.Default.GetBytes(desc1).Length;
int remainLength2 = total2 - System.Text.Encoding.Default.GetBytes(desc2).Length;
return desc1 + "".PadRight(remainLength, " "[0]) + desc2 + "".PadRight(remainLength2, " "[0]);
}
///
/// 根据targetStr长度设置
///
///
///
public static string GetFixedStr2(string desc1, string targetStr)
{
int targetLength = System.Text.Encoding.Default.GetBytes(targetStr).Length;
int useLength = System.Text.Encoding.Default.GetBytes((desc1 + "")).Length;
int remainLength = targetLength - useLength;
if (remainLength < 0)
{
remainLength = 0;
}
return desc1 + "".PadRight(remainLength, " "[0]);
}
///
/// 以精确模式转换浮点数为特定小数精度的字符串,使用四舍五入模式,
/// 但111111111111.2249999999999999需要转为111111111111.22而不是111111111111.23
///
public static string ToFixedPrecise(double? value, int decimalSize = 2)
{
if (value == null)
{
return string.Empty;
}
if (decimalSize < 1)
{
return Math.Floor(value.Value).ToString("0");
}
var decExp = Math.Pow(10, decimalSize + 1);
return (Math.Floor(value.Value * decExp) / decExp).ToString("f" + decimalSize);
}
///
/// 将逗号分隔的字符串转换为字符串数组,并去除空白项与重复项。
///
/// 逗号分隔的字符串。
/// 去除空白项与重复项后的字符串数组。
public static string[] ConvertCommaValuesToStringArray(string commaValues)
{
if (string.IsNullOrWhiteSpace(commaValues))
{
return new string[0];
}
return commaValues.Split(',')
.Select(o => o?.Trim())
.Where(o => !string.IsNullOrWhiteSpace(o))
.Distinct()
.ToArray();
}
}
}