using System.Collections.Generic;
using System.Text;

namespace Fort23.Core
{
    public static class StringHelper
    {
        public static int ToInt(this string value)
        {
            int result;
            int.TryParse(value, out result);
            return result;
        }

        public static long ToLong(this string value)
        {
            long result;
            long.TryParse(value, out result);
            return result;
        }

        /// <summary>
        /// 将list中所有项连接成字符串，sign 连接符
        /// </summary>
        public static string SplitToString<T>(this List<T> list, string sign = "")
        {
            StringBuilder str = new StringBuilder();
            for (var i = 0; i < list.Count; i++)
            {
                str.Append(list[i]);
                if (i != list.Count - 1)
                    str.Append(sign);
            }

            return str.ToString();
        }

        /// <summary>
        /// 将array中所有项连接成字符串，sign 连接符
        /// </summary>
        public static string SplitToString<T>(this T[] array, string sign = "")
        {
            StringBuilder str = new StringBuilder();
            for (var i = 0; i < array.Length; i++)
            {
                str.Append(array[i]);
                if (i != array.Length - 1)
                    str.Append(sign);
            }

            return str.ToString();
        }

        public static float[] ToFloats(this int[] array)
        {
            float[] result = new float[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = array[i];
            }

            return result;
        }

        /// <summary>
        /// 价格转换成带货币符号的字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToCurrencyString(this int value)
        {
            return $"￥{value}";
        }
        
        /// <summary>
        /// 价格转换成带货币符号的字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToCurrencyString(this float value)
        {
            return $"￥{value}";
        }
    }
}