欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net 字符串操作基类(安全,替换,分解等)

程序员文章站 2023-08-23 19:24:04
/********************************************************************************** *...
/**********************************************************************************
*
* 功能说明:常用函数基类
* 作者: 刘功勋;
* 版本:v0.1(c#2.0);时间:2006-8-13
*
* *******************************************************************************/
/***************************************************************
* 更新记录
* 2007-1-5 更新:
* 1,取字符串右侧的几个字符
* 2,替换右侧的字符串
****************************************************************/
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.text;
namespace ec
{
/// <summary>
/// 常用函数基类
/// </summary>
public class funobject
{
#region 替换字符串
/// <summary>
/// 功能:替换字符
/// </summary>
/// <param name="strvalue">字符串</param>
/// <returns>替换掉'的字符串</returns>
public static string filtersql(string strvalue)
{
string str = "";
str = strvalue.replace("''", "");
return str;
}
#endregion
#region 对表 表单内容进行转换html操作,
/// <summary>
/// 功能:对表 表单内容进行转换html操作,
/// </summary>
/// <param name="fstring">html字符串</param>
/// <returns></returns>
public static string htmlcode(string fstring)
{
string str = "";
str = fstring.replace(">", ">");
str = fstring.replace("<", "<");
str = fstring.replace(" ", " ");
str = fstring.replace("\n", "<br />");
str = fstring.replace("\r", "<br />");
str = fstring.replace("\r\n", "<br />");
return str;
}
#endregion
#region 判断是否:返回值:√ or ×
/// <summary>
/// 判断是否:返回值:√ or ×
/// </summary>
/// <param name="b">true 或false</param>
/// <returns>√ or ×</returns>
public static string judgement(bool b)
{
string s = "";
if (b == true)
s = "<b><font color=#009900>√</font></b>";
else
s = "<b><font color=#ff0000>×</font></b>";
return s;
}
#endregion
#region 截取字符串
/// <summary>
/// 功能:截取字符串长度
/// </summary>
/// <param name="str">要截取的字符串</param>
/// <param name="length">字符串长度</param>
/// <param name="flg">true:加...,flase:不加</param>
/// <returns></returns>
public static string getstring(string str, int length, bool flg)
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
str = str.substring(0, j);
if (flg)
str += "......";
break;
}
j++;
}
return str;
}
#endregion
#region 截取字符串+…
/// <summary>
/// 截取字符串+…
/// </summary>
/// <param name="strinput"></param>
/// <param name="intlen"></param>
/// <returns></returns>
public static string cutstring(string strinput, int intlen)//截取字符串
{
asciiencoding ascii = new asciiencoding();
int intlength = 0;
string strstring = "";
byte[] s = ascii.getbytes(strinput);
for (int i = 0; i < s.length; i++)
{
if ((int)s[i] == 63)
{
intlength += 2;
}
else
{
intlength += 1;
}
try
{
strstring += strinput.substring(i, 1);
}
catch
{
break;
}
if (intlength > intlen)
{
break;
}
}
//如果截过则加上半个省略号
byte[] mybyte = system.text.encoding.default.getbytes(strinput);
if (mybyte.length > intlen)
{
strstring += "…";
}
return strstring;
}
#endregion
#region 字符串分函数
/// <summary>
/// 字符串分函数
/// </summary>
/// <param name="strid"></param>
/// <param name="index"></param>
/// <param name="separ"></param>
/// <returns></returns>
public string stringsplit(string strings, int index, string separ)
{
string[] s = strings.split(char.parse(separ));
return s[index];
}
#endregion
#region 分解字符串为数组
/// <summary>
/// 字符串分函数
/// </summary>
/// <param name="str">要分解的字符串</param>
/// <param name="splitstr">分割符,可以为string类型</param>
/// <returns>字符数组</returns>
public static string[] splitstr(string str, string splitstr)
{
if (splitstr != "")
{
system.collections.arraylist c = new system.collections.arraylist();
while (true)
{
int thissplitindex = str.indexof(splitstr);
if (thissplitindex >= 0)
{
c.add(str.substring(0, thissplitindex));
str = str.substring(thissplitindex + splitstr.length);
}
else
{
c.add(str);
break;
}
}
string[] d = new string[c.count];
for (int i = 0; i < c.count; i++)
{
d[i] = c[i].tostring();
}
return d;
}
else
{
return new string[] { str };
}
}
#endregion
#region url编码
/// <summary>
/// url编码
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public static string urlencoding(string str)
{
byte[] bytes = system.text.encoding.utf8.getbytes(str);
return system.text.encoding.utf8.getstring(bytes).tostring();
}
#endregion
#region 获取web.config中的配置字段值
/// <summary>
/// 获取全局配置参数
/// </summary>
/// <param name="key">键名</param>
/// <returns>参数</returns>
public static string getapp(string key)
{
system.configuration.appsettingsreader appr = new system.configuration.appsettingsreader();
try
{
string str = (string)appr.getvalue(key, typeof(string));
if (str == null || str == "") return null;
return str;
}
catch (exception e) { }
return null;
}
#endregion
#region 根据传入的字符串是否为yes/no返回bit
/// <summary>
/// 根据传入的字符串是否为yes/no返回bit
/// </summary>
/// <param name="flg"></param>
/// <returns></returns>
public static int getbitbool(string flg)
{
int str = 0;
switch (flg.tolower())
{
case "yes":
str = 1;
break;
case"no":
str = 0;
break;
default:
break;
}
return str;
}
#endregion
#region html编码
/// <summary>
/// html编码
/// </summary>
/// <param name="strinput"></param>
/// <returns></returns>
public static string htmlencode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmlencode(strinput);
}
catch
{
str = "error";
}
return str;
}
/// <summary>
/// html解码
/// </summary>
/// <param name="strinput"></param>
/// <returns></returns>
public static string htmldecode(string strinput)
{
string str;
try
{
str = httpcontext.current.server.htmldecode(strinput);
}
catch
{
str = "error";
}
return str;
}
#endregion
#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
/// <summary>
/// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
/// </summary>
/// <param name="srcstring">原始字符串</param>
/// <param name="aimstring">目标字符串</param>
/// <returns></returns>
public static bool isenglish(string srcstring, string aimstring)
{
bool rev = true;
string chr;
if (aimstring == "" || aimstring == null) return false;
for (int i = 0; i < aimstring.length; i++)
{
chr = aimstring.substring(i, 1);
if (srcstring.indexof(chr) < 0)
{
return false;
break;
}
}
return rev;
}
#endregion
#region 检测字符串中是否含有中文及中文长度
/// <summary>
/// 检测字符串中是否含有中文及中文长度
/// </summary>
/// <param name="str">要检测的字符串</param>
/// <returns>中文字符串长度</returns>
public static int cnstringlength(string str)
{
asciiencoding n = new asciiencoding();
byte[] b = n.getbytes(str);
int l = 0; // l 为字符串之实际长度
for (int i = 0; i <= b.length - 1; i++)
{
if (b[i] == 63) //判断是否为汉字或全脚符号
{
l++;
}
}
return l;
}
#endregion
#region 取字符串右侧的几个字符
/// <summary>
/// 取字符串右侧的几个字符
/// </summary>
/// <param name="str">字符串</param>
/// <param name="length">右侧的几个字符</param>
/// <returns></returns>
public static string getstrright(string str, int length)
{
string rev = "";
if (str.length < length)
{
rev = str;
}
else
{
rev = str.substring(str.length - length, length);
}
return rev;
}
#endregion
#region 替换右侧的字符串
/// <summary>
/// 替换右侧的字符串
/// </summary>
/// <param name="str">字符串</param>
/// <param name="strsrc">右侧的字符串</param>
/// <param name="straim">要替换为的字符串</param>
/// <returns></returns>
public static string repstrright(string str, string strsrc, string straim)
{
string rev = "";
if (getstrright(str, strsrc.length) != strsrc)
{
rev = str;
}
else
{
rev = str.substring(0, str.length - strsrc.length).tostring() + straim.tostring();
}
return rev;
}
#endregion
}
}

上一篇: 怎么挑西瓜

下一篇: 2、css-过渡