文本转换工具类

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

namespace InterfaceCommon.Common
{
    /// <summary>
    /// 文本操作工具类
    /// </summary>
    public static class StringUtil
    {

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="value">要判断的字符串</param>
        /// <returns>true 为空; false 不为空</returns>
        public static bool Empty(string value)
        {
            return value == null || value.Trim().Length == 0;
        }

        /// <summary>
        /// 判断字符串是否不为空
        /// </summary>
        /// <param name="value">要判断的字符串</param>
        /// <returns>true 不为空; false 为空</returns>
        public static bool NotEmpty(string value)
        {
            return !Empty(value);
        }

        /// <summary>
        /// 将null的字符串转换为空字符串
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string NullToSpace(string value)
        {
            return value == null ? "" : value;
        }

        /// <summary>
        /// 将字符串中的匹配项替换为指定的字符串
        /// </summary>
        /// <param name="value">要替换的字符串</param>
        /// <param name="find">匹配项</param>
        /// <param name="replace">替换字符串</param>
        /// <returns>替换后的字符串</returns>
        public static string Replace(string value, string find, string replace)
        {
            if (value == null)
            {
                return null;
            }
            return value.Replace(find, replace);
        }

        /// <summary>
        /// 以指定的字符串为分隔符分割字符串
        /// </summary>
        /// <param name="value">要分割的字符串</param>
        /// <param name="split">分隔符字符串</param>
        /// <returns>分割后的字符串数组</returns>
        public static string[] Split(string value, string split)
        {
            if (value == null)
            {
                return null;
            }
            return value.Split(split.ToCharArray());
        }

        /// <summary>
        /// 将字符串转换为整型数据
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的整形数据</returns>
        public static int ToInt(string value)
        {
            if (Empty(value))
            {
                return 0;
            }
            try
            {
                return Int32.Parse(value);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// 将对象转换为整型数据
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>转换后的整形数据</returns>
        public static int ToInt(object value)
        {
            string valueString = value.ToString();
            if (Empty(valueString))
            {
                return 0;
            }
            try
            {
                return Int32.Parse(valueString);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// 将字符串转换为长整型数据
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的整形数据</returns>
        public static long ToLong(string value)
        {
            if (Empty(value))
            {
                return 0L;
            }
            try
            {
                return Int64.Parse(value);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0L;
            }
        }

        /// <summary>
        /// 将对象转换为长整型数据
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>转换后的整形数据</returns>
        public static long ToLong(object value)
        {
            string valueString = value.ToString();
            if (Empty(valueString))
            {
                return 0L;
            }
            try
            {
                return Int64.Parse(valueString);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0L;
            }
        }

        /// <summary>
        /// 将字符串转换为单精度数据
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的整形数据</returns>
        public static float ToFloat(string value)
        {
            if (Empty(value))
            {
                return 0.0F;
            }
            try
            {
                return Single.Parse(value);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0.0F;
            }
        }

        /// <summary>
        /// 将对象转换为单精度数据
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>转换后的整形数据</returns>
        public static float ToFloat(object value)
        {
            string valueString = value.ToString();
            if (Empty(valueString))
            {
                return 0.0F;
            }
            try
            {
                return Single.Parse(valueString);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0.0F;
            }
        }

        /// <summary>
        /// 将字符串转换为双精度数据
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的整形数据</returns>
        public static double ToDouble(string value)
        {
            if (Empty(value))
            {
                return 0.0D;
            }
            try
            {
                return Double.Parse(value);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0.0D;
            }
        }

        /// <summary>
        /// 将对象转换为双精度数据
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>转换后的整形数据</returns>
        public static double ToDouble(object value)
        {
            string valueString = value.ToString();
            if (Empty(valueString))
            {
                return 0.0D;
            }
            try
            {
                return Double.Parse(valueString);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0.0D;
            }
        }

        /// <summary>
        /// 将字符串转换为十进制数
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的十进制数</returns>
        public static decimal ToDecimal(string value)
        {
            if (Empty(value))
            {
                return 0M;
            }
            try
            {
                return Decimal.Parse(value);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0M;
            }
        }

        /// <summary>
        /// 将对象转换为十进制数
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>转换后的十进制数</returns>
        public static decimal ToDecimal(object value)
        {
            string valueString = value.ToString();
            if (Empty(valueString))
            {
                return 0M;
            }
            try
            {
                return Decimal.Parse(valueString);
            }
            catch (Exception ex)
            {
                Log.warn(ex.Message);
                return 0M;
            }
        }



        public static string getFormat0CmdtyOrderCode(string oldCmdtyOrderCode)
        {

            string newCmdtyOrderCode = "";

            if (oldCmdtyOrderCode.Length == 7)
            {
                oldCmdtyOrderCode = "0" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 6)
            {
                oldCmdtyOrderCode = "00" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 5)
            {
                oldCmdtyOrderCode = "000" + oldCmdtyOrderCode;
            }

            newCmdtyOrderCode = oldCmdtyOrderCode;

            return newCmdtyOrderCode;

        }



        public static string getFormat0CmdtyOrderCodeNew(string oldCmdtyOrderCode)
        {

            string newCmdtyOrderCode = "";

            if (oldCmdtyOrderCode.Length == 12)
            {
                oldCmdtyOrderCode = "0" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 11)
            {
                oldCmdtyOrderCode = "00" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 10)
            {
                oldCmdtyOrderCode = "000" + oldCmdtyOrderCode;
            }

            newCmdtyOrderCode = oldCmdtyOrderCode;

            return newCmdtyOrderCode;

        }




        public static string getFormatCmdtyOrderCode(string oldCmdtyOrderCode)
        {

            string newCmdtyOrderCode = "";

            if (oldCmdtyOrderCode.Length == 7)
            {
                oldCmdtyOrderCode = "0" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 6)
            {
                oldCmdtyOrderCode = "00" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 5)
            {
                oldCmdtyOrderCode = "000" + oldCmdtyOrderCode;
            }

            if (oldCmdtyOrderCode.Length == 8)
            {
                newCmdtyOrderCode = oldCmdtyOrderCode.Substring(0, 3) + "-" + oldCmdtyOrderCode.Substring(3, 5);
            }
            else
            {
                newCmdtyOrderCode = oldCmdtyOrderCode;
            }

            return newCmdtyOrderCode;

        }


        public static string getWMSCmdtyOrderCode(string oldCmdtyOrderCode)
        {

            string tempCode = "";
            int tempString = 0;
            int tempString1 = 0;
            int tempString2 = 0;
            string newCmdtyOrderCode = "";

            if (oldCmdtyOrderCode.Length == 7)
            {
                oldCmdtyOrderCode = "0" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 6)
            {
                oldCmdtyOrderCode = "00" + oldCmdtyOrderCode;
            }
            else if (oldCmdtyOrderCode.Length == 5)
            {
                oldCmdtyOrderCode = "000" + oldCmdtyOrderCode;
            }

            if (oldCmdtyOrderCode.Length == 8)
            {
                tempCode = oldCmdtyOrderCode + "00000";

                tempString1 = Int32.Parse((tempCode.Substring(0, 1)))
                            + Int32.Parse((tempCode.Substring(2, 1)))
                            + Int32.Parse((tempCode.Substring(4, 1)))
                            + Int32.Parse((tempCode.Substring(6, 1)))
                            + Int32.Parse((tempCode.Substring(8, 1)))
                            + Int32.Parse((tempCode.Substring(10, 1)))
                            + Int32.Parse((tempCode.Substring(12, 1)));

                tempString2 = Int32.Parse((tempCode.Substring(1, 1)))
                            + Int32.Parse((tempCode.Substring(3, 1)))
                            + Int32.Parse((tempCode.Substring(5, 1)))
                            + Int32.Parse((tempCode.Substring(7, 1)))
                            + Int32.Parse((tempCode.Substring(9, 1)))
                            + Int32.Parse((tempCode.Substring(11, 1)));

                tempString2 = tempString2 * 3;
                tempString = tempString1 + tempString2;

                if (tempString.ToString().Length == 3)
                {
                    tempString = (Int32.Parse(tempString.ToString().Substring(0, 2)) + 1) * 10 - tempString;
                }
                else if (tempString.ToString().Length == 1)
                {
                    tempString = 10 - Int32.Parse(tempString.ToString());
                }
                else
                {
                    tempString = (Int32.Parse(tempString.ToString().Substring(0, 1)) + 1) * 10 - tempString;
                }



                if (tempString == 10)
                {
                    tempString = 0;
                }

                newCmdtyOrderCode = oldCmdtyOrderCode + "0000" + tempString.ToString();
            }
            else
            {
                newCmdtyOrderCode = oldCmdtyOrderCode;
            }

            return newCmdtyOrderCode;

        }



        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>        
        public static string ToSBC(string input)
        {
            //半角转全角:
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32 || c[i] == 13 || c[i] == 10)
                {
                    c[i] = (char)12288;
                    continue;
                }


                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }


        /// <summary>
        /// 判断日期型函数
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>true;false</returns>
        ///<remarks>
        ///</remarks> 
        public static bool isDate(string strDate)
        {
            bool checkFlg = false;
            try
            {
                Convert.ToDateTime(strDate);
                checkFlg = true;
            }
            catch
            {
                checkFlg = false;
            }

            return checkFlg;

        }


        /// <summary>
        /// 判断数字型函数
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>true;false</returns>
        ///<remarks>
        public static bool isNumberic(string oText)
        {
            try
            {
                int var1 = Convert.ToInt32(oText);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断数字型函数(可小数)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>true;false</returns>
        ///<remarks>
        public static bool isDecimal(string oText)
        {
            try
            {
                double var1 = Convert.ToDouble(oText);
                if (var1 < 0)
                {
                    Log.error(oText);
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 字段拆分
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>true;false</returns>
        ///<remarks>
        public static string[] split38byte(string address)
        {
            string[] splitAddress = { "", "", "" };
            int length = address.Length;
            for (int i = 0; i < 3; i++)
            {
                if (length > 38 * (i + 1))
                {
                    splitAddress[i] = address.Substring(38 * i, 38);
                }
                else
                {
                    splitAddress[i] = address.Substring(38 * i, length - (38 * i));
                    break;
                }
            }
            return splitAddress;
        }

        /// <summary>
        /// 将空字符串转换为null的字符串
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string SpaceToNull(string value)
        {
            if (value.Equals("''"))
            {
                value = "null";
            }
            return value;
        }


        //日期格式转成YYYYMMDD
        public static string forMatDate(string value)
        {
            string lastDate = value.Substring(0, 4) + value.Substring(5, 2) + value.Substring(8, 2);
            return lastDate;
        }

        //日期格式转成YYYYYYMMDDhhmmss
        public static string forMatDateYYYYMMDDhhmmss(string value)
        {
           // string crrent = "2016/9/6 0:00:00.000";
            char[] arrChar = { '/', ':', '-', ' ', '.' };
            string[] list = value.Split(arrChar);
            //[2016,9,6,0,00,00,000]
            if(list[1].Length!=2){
                list[1] = "0" + list[1];
            }
            if (list[2].Length != 2)
            {
                list[2] = "0" + list[2];
            }
            if (list[3].Length != 2)
            {
                list[3] = "0" + list[3];
            }
            value = list[0] + list[1] + list[2] + list[3] + list[4] + list[5];
           /* string[] ms = value.Split(new[] { "." }, StringSplitOptions.None); //将毫秒去掉

            string[] ArrayList = ms[0].Split(new[] { " " }, StringSplitOptions.None); //将年月日和时分秒 分隔开

            string[] year = ArrayList[0].Split(new[] { "/" }, StringSplitOptions.None);//年月日

            if (year[1].Length == 1)
            {
                year[1] = "0" + year[1];
            }
            if (year[2].Length == 1)
            {
                year[2] = "0" + year[2];
            }

            string[] min = ArrayList[1].Split(new[] { ":" }, StringSplitOptions.None);//时分秒

            if (min[0].Length == 1)
            {
                min[0] = "0" + min[0];
            }
            value = year[0] + year[1] + year[2] + min[0] + min[1] + min[2];*/
            //string lastDate = value.Substring(0, 4) + value.Substring(5, 2) + value.Substring(8, 2) + value.Substring(11, 2) + value.Substring(14, 2) + value.Substring(17, 2);
            return value;
        }
        //地址切割
        public static string formatAddress(string address1, string address2, string address3, string address4)
        {
            string address = address1 + address2 + address3 + address4;
            if ((address1.Length + address2.Length + address3.Length + address4.Length) > 114)
            {
                address = address.Substring(0, 114);
            }

            return address;

        }
        //2014/05/15 K0048 LCS ADD START
        /// <summary>
        /// 获取json字段的值
        /// </summary>
        /// <param name="jsonProperty">json字段</param>
        /// <param name="type">值类型</param>
        /// <returns>json字段的值</returns>
        public static string getJsonValue(JsonProperty jsonProperty, string type)
        {
            if (jsonProperty == null)
            {
                switch (type)
                {
                    case "string":
                        return "";
                    case "number":
                        return "0";
                    default:
                        return "";
                }
            }
            else
            {
                switch (type)
                {
                    case "date":
                        if (jsonProperty.Value.Equals("0001-01-0100:00:00"))
                        {
                            return "1970-01-01 00:00:00";
                        }
                        return jsonProperty.Value.Insert(10, " ");
                    default:
                        return jsonProperty.Value;
                }
            }
        }

        /// <summary>
        /// 验证返回信息
        /// </summary>
        /// <param name="responseJObject">json内容</param>
        /// <returns>出错信息</returns>
        public static string validate_Send_Result(JsonObject responseJObject)
        {
            string errorMsg = "";
            if (responseJObject["error_response"] != null)
            {
                errorMsg = responseJObject["error_response"]["zh_desc"].ToString();
            }
            return errorMsg;
        }
        //2014/05/15 K0048 LCS ADD END

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值