using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbCommon.Extenions
{
    public static class StringExtenions
    {
        public static int[] ToIntArray(this string str)
        {
            return string.IsNullOrEmpty(str) ? new int[0] :
                str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s =>
                {
                    int mark = 0;
                    return int.TryParse(s, out mark) ? mark : 0;
                })
                .ToArray();
        }
        public static string[] ToStringArray(this string str)
        {
            return string.IsNullOrEmpty(str) ? new string[0] :
                str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                .ToArray();
        }
        public static string[] ToPosArray(this string str)
        {
            return Function.Tools.StringToPosArray(str);
        }

        public static string ToPosString(this string[] strArray)
        {
            return Function.Tools.PosArrayToString(strArray);
        }

        public static string ToChineseString(this bool value)
        {
            return value ? "成功" : "失败";
        }

        public static bool[] ToBoolArray(this string str)
        {
            return string.IsNullOrEmpty(str) ? new bool[0] :
                str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s =>
                {
                    return s == "1" || s == "True" || s == "true" ? true : false;
                })
                .ToArray();
        }

        public static int ToInt(this string str)
        {
            return string.IsNullOrEmpty(str) ? 0 
                : (int.TryParse(str, out int mark) ? mark : 0);
        }
        public static double ToDouble(this string str)
        {
            return string.IsNullOrEmpty(str) ? 0.0
                : (double.TryParse(str, out double mark) ? mark : 0.0);
        }
    }
}
