asp.net字串處理類代碼

來源:互聯網
上載者:User

複製代碼 代碼如下: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
{
/// <summary>
/// 去掉字串中的所有空格
/// </summary>
/// <param name="_str"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 截取字串並限制字串長度,多於給定的長度+。。。
/// </summary>
/// <param name="str">待截取的字串</param>
/// <param name="len">每行的長度,多於這個長度自動換行</param>
/// <param name="max">輸出字串最大的長度</param>
/// <returns></returns>
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) + "<br>";
}
}
else if (i > last)
{
s += str.Substring(i - 1);
break;
}
}
return s + sheng;
}
/// <summary>
/// 截取字串,不限制字串長度
/// </summary>
/// <param name="str">待截取的字串</param>
/// <param name="len">每行的長度,多於這個長度自動換行</param>
/// <returns></returns>
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) + "<br>";
}
}
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;
}
/// <summary>
///這個方法確保使用者的輸入不是惡毒的
/// </summary>
/// <param name="text">輸入字串</param>
/// <param name="maxLength">最大長度</param>
/// <returns>轉換後的字串</returns>
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"); //<br>
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;
}
/// <summary>
/// 字串中大寫字元轉小寫
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="str"></param>
/// <param name="key">必須是8位的字串</param>
/// <returns></returns>
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();
}
/// <summary>
/// Des 解密 GB2312
/// </summary>
/// <param name="str">Desc string</param>
/// <param name="key">Key ,必須為8位 </param>
/// <returns></returns>
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());
}
/// <summary>
/// MD5無法復原加密 32位
/// </summary>
/// <param name="s"></param>
/// <param name="_input_charset"></param>
/// <returns></returns>
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;
}
/// <summary>
/// MD5無法復原加密 16位
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
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;
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.