当前位置: 编程技术>.net/c#/asp.net
asp.net字符串处理类代码
来源: 互联网 发布时间:2014-08-30
本文导语: asp.net字符串处理类代码,供大家学习参考。 代码如下: 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 ...
asp.net字符串处理类代码,供大家学习参考。
代码如下:
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)
{
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)
{
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, "()+|()", "n"); //
text = Regex.Replace(text, "(\s*&[n|N][b|B][s|S][p|P];\s*)+", " "); //
text = Regex.Replace(text, "", 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])
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)
{
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)
{
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, "()+|()", "n"); //
text = Regex.Replace(text, "(\s*&[n|N][b|B][s|S][p|P];\s*)+", " "); //
text = Regex.Replace(text, "", 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])