编程菜鸟到大鸟--代码积累

  在进行编码的过程中,我们可以会遇到很多的例如数组与字母互转,或者一些字符串的积累,为了以后方便代码的查找,特意形成自己代码库,也希望可以帮到其他人呢。

 /// 
        /// 将数组中的内容以delimiter为间隔拼接字符串
        /// 
        /// 
        /// 
        /// 
        public static string join(object[] array, String delimiter)
        {
            if (array == null || array.Length == 0)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            foreach (object item in array)
            {
                builder.Append(item + delimiter);
            }
            builder.Remove(builder.Length - 1,1);
            return builder.ToString();
        }

        /// 
        /// 去除字符串中的所有引号
        /// 
        /// 
        /// 
        public static string RemoveStringQuotationMarks(string str)
        {
            return RemoveStringQuotationMarks(str, true);
        }

        /// 
        /// 去除字符串中的引号
        /// 
        /// 
        /// 
        /// 
        public static string RemoveStringQuotationMarks(string str, bool isAll)
        {
            if (!String.IsNullOrEmpty(str))
            {
                str = str.Replace("\r", "");
                str = str.Replace("\n", "");
                str = str.Replace("'", "’");
                if (isAll)
                {
                    str = str.Replace("\"", "”");
                }
            }
            return str;
        }

        public static string ConvertByteToString(byte[] bytes)
        {
            if(bytes == null || bytes.Length == 0)
            {
                return "";
            }
            return Encoding.Default.GetString(bytes);
        }

        public static byte[] ConvertStringToByte(string str)
        {
            if(string.IsNullOrEmpty(str))
            {
                return null;
            }
            return Encoding.Default.GetBytes(str);
        }

        /// 
        /// 将以逗号分开的字符串转换为decimal数组
        /// 
        /// 
        /// 
        public static decimal[] ConvertStrToNum(string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            string[] strs = str.Split(',');
            List<decimal> list = new List<decimal>();
            foreach (var s in strs)
            {
                decimal d;
                decimal.TryParse(s, out d);
                list.Add(d);
            }
            return list.ToArray();
        }

        /// 
        /// 将字符串内容转化为16进制数据编码
        /// 
        /// 
        /// 
        public static string Conv_Strto16(string strEncode)
        {
            string strReturn = ""; //  存储转换后的编码
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X4");
            }
            return strReturn;
        }

        /// 
        /// 将16进制数据编码转化为字符串
        /// 
        /// 
        /// 
        public static string Conv_16toStr(string strDecode)
        {
            string sResult = "";
            for (int i = 0; i < strDecode.Length / 4; i++)
            {
                sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), System.Globalization.NumberStyles.HexNumber);
            }
            return sResult;
        }
  //将数字转换成字母
        public static string Num2Alpha(int index)
        {
            if (index < 26)
            {
                return ((char)('A' + index)).ToString(CultureInfo.InvariantCulture);
            }
            return Num2Alpha(index / 26 - 1) + Num2Alpha(index % 26);
        }
        //将字母转换为数字
        public static int Alpha2Num(string str)
        {
            str = str.ToUpper();
            var n = 0;
            for (var i = 0; i < str.Length; i++)
            {
                n = n * 26 + Convert.ToChar(str[i]) - 0x40;
            }
            return n - 1;
        }

        /// 
        /// 取字符串中数字
        /// 
        /// 
        /// 
        public static string GetNumFromString(string str)
        {
            if (string.IsNullOrEmpty(str))
                return ""; 
            const string num = "0123456789.±-~";
            string temp = string.Empty;
            char[] cs = str.Trim().ToCharArray();
            foreach (var c in cs)
            {
                if (num.Contains(c.ToString())) temp += c;
            }
            return temp;
        }

你可能感兴趣的:(代码库)