asp.net字符串处理类代码
网络编程 2021-07-04 22:40www.168986.cn编程入门
asp.字符串处理类代码,需要的朋友可以参考下
代码如下:
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;
}
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程