设为首页 | 加入收藏

网站建设报价咨询,网站建设A套餐 网站建设报价咨询,网站建设B套餐 网站建设报价咨询,网站建设C套餐 网站建设报价咨询
网站建设知识
网络整合营销外包,长沙网站建设
您目前的位置:森达网络 >>网站建设知识
网站建设知识

长沙做网站公司如何封装asp.net字符串处理类

本文来源: asp.net字符串处理      本文作者:asp.net字符串处理类      发布时间:2017-09-14


长沙做网站公司在网站建设方面已有多年经验,考虑到开发的效率我们会将经常使用到的方法类进行封装,以便于组员多次使用,我们开发的系统都是我们自己纯手工编码技术开发,对于其它做网站的使用网上源码或者CMS建站方式更加具有安全性和可扩展性。 下面的代码是我们经常使用的封装代码,现在分享出来和大家交流一下,以便更好的改进我们的开发效率。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace StringClass
{
    public class StringHelper
    {
        ///


        /// 去掉字符串中的所有空格
        ///

        ///

        ///

        public static string ReMoveBlank(string _str)
        {
            string strTemp = "";
            CharEnumerator CEnumerator = _str.GetEnumerator();
            while (CEnumerator.MoveNext())
            {
                byte[] array = new byte[1];
                array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
                int asciicode = (short)(array[0]);
                if (asciicode != 32)
                {
                    strTemp += CEnumerator.Current.ToString();
                }
            }
            return strTemp;
        }

 


        ///


        /// 截取字符串并限制字符串长度,多于给定的长度+。。。
        ///

        ///
待截取的字符串
        ///
每行的长度,多于这个长度自动换行
        ///
输出字符串最大的长度
        ///

        public static string CutStr(string str, int len, int max)
        {
            string s = "";
            string sheng = "";
            if (str.Length > max)
            {
                str = str.Substring(0, max);
                sheng = "";
            }
            for (int i = 0; i < str.Length; i++)
            {
                int r = i % len;
                int last = (str.Length / len) * len;
                if (i != 0 && i <= last)
                {
                    if (r == 0)
                    {
                        s += str.Substring(i - len, len) + "
";
                    }

 

                }
                else if (i > last)
                {
                    s += str.Substring(i - 1);
                    break;
                }

            }
            return s + sheng;
        }

        ///


        /// 截取字符串,不限制字符串长度
        ///

        ///
待截取的字符串
        ///
每行的长度,多于这个长度自动换行
        ///

        public static string CutStr(string str, int len)
        {
            string s = "";
            for (int i = 0; i < str.Length; i++)
            {
                int r = i % len;
                int last = (str.Length / len) * len;
                if (i != 0 && i <= last)
                {
                    if (r == 0)
                    {
                        s += str.Substring(i - len, len) + "
";
                    }
                }
                else if (i > last)
                {
                    s += str.Substring(i - 1);
                    break;
                }
            }
            return s;
        }

 

        public static string PartSubString(string str, int len)
        {
            if (str.Length > len)
            {
                return str.Substring(0, len) + "...";
            }
            return str;
        }

        ///


        ///这个方法确保用户的输入不是恶毒的
        ///

        ///
输入字符串
        ///
最大长度
        ///
转换后的字符串
        public static string InputText(string text, int maxLength)
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            if (text.Length > maxLength)
                text = text.Substring(0, maxLength);
            text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //

            text = Regex.Replace(text, "(
\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
            text = text.Replace("’", "’’");
            return text;
        }

 

        ///


        /// 字符串中大写字符转小写
        ///

        ///

        ///

        public static string StringToLower(string str)
        {
            Char[] a = str.ToCharArray();
            string strTemp = "";
            for (int i = 0; i < a.Length; i++)
            {
                if (Convert.ToInt32(a[i]) >= 65 && Convert.ToInt32(a[i]) <= 90)
                    strTemp += a[i].ToString().ToLower();
                else
                    strTemp += a[i].ToString();
            }
            return strTemp;
        }

 

        ///

  
        /// 加密  
        ///
  
        ///
  
        ///
必须是8位的字符串  
        ///
  
        public static string Encode(string str, int keyIndex)
        {
            ArrayList alKey = new ArrayList();
            alKey.Add("
BookT@#+!NumBq2");
            alKey.Add("
MagaZine@(21&*ID5");
            alKey.Add("ThesisDSHI}._Y");
            string key = alKey[keyIndex].ToString();
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            stream.Close();
            return builder.ToString();
        }

 

        ///

  
        /// Des 解密 GB2312   
        ///
  
        ///
Desc string  
        ///
Key ,必须为8位   
        ///
  
        public static string Decode(string str, int keyIndex)
        {
            ArrayList alKey = new ArrayList();
            alKey.Add("
BookT@#+!NumBq2");
            alKey.Add("
MagaZine@(21&*ID5");
            alKey.Add("ThesisDSHI}._Y");
            string key = alKey[keyIndex].ToString();
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            byte[] buffer = new byte[str.Length / 2];
            for (int i = 0; i < (str.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte)num2;
            }
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            stream.Close();
            return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
        }

 

        ///


        /// MD5不可逆加密 32位
        ///

        ///

        ///

        ///

        public static string GetMD5_32(string str1)
        {
            string cl1 = str1;
            string pwd = "";
            MD5 md5 = MD5.Create();
            // 加密后是一个字节类型的数组
            byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
            // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
                pwd = pwd + s[i].ToString("x");
            }
            return pwd;
        }
        ///

        /// MD5不可逆加密 16位
        ///

        ///

        ///

        public static string GetMd5_16(string ConvertString)
        {

 

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);

            t2 = t2.Replace("-", "");

            return t2;

        }
    }
}
本文由
长沙企业网站建设整理,转载请注明www.sendawangluo.com

版权所有:长沙森达网络工作室2011-2016 All Rights Reserved 地址:长沙市岳麓区中电软件园总部大楼 网站备案号:湘ICP备11005944号