Asp.net 字符串操作基类(安全,替换,分解等)
/############################################
版權聲明:
文章內容為本站編輯,創作.你可以任意轉載、發布、使用但請務必明文標注文章原始出處及本聲明
http://www.opent.cn? 作者:浪淘沙
############################################/
/**********************************************************************************
?*
?* 功能說明:常用函數基類
?* 作者: 劉功勛;
?* 版本: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
??? }
}
總結
以上是生活随笔為你收集整理的Asp.net 字符串操作基类(安全,替换,分解等)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [AH2017/HNOI2017]礼物(
- 下一篇: Matlab概率统计编程指南