午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    C#字符串工具類截取過濾格式判斷等
    來源:易賢網(wǎng) 閱讀:995 次 日期:2014-08-20 14:55:12
    溫馨提示:易賢網(wǎng)小編為您整理了“C#字符串工具類截取過濾格式判斷等”,方便廣大網(wǎng)友查閱!

    C#字符串工具類,實現(xiàn)的功能包括:判斷某值是否在枚舉內(nèi)(位枚舉)、將全角數(shù)字轉(zhuǎn)換為數(shù)字、判斷是否為IP、獲得當(dāng)前頁面客戶端的IP、改正sql語句中的轉(zhuǎn)義字符、檢測是否是正確的Url、檢測是否符合email格式、SQL字符串過濾、按字節(jié)數(shù)截取字符串(不帶省略號)、按字節(jié)數(shù)截取字符串(后面加省略號...)等。

    view sourceprint?001using System;

    002using System.Collections.Generic;

    003using System.Linq;

    004using System.Text;

    005using System.Text.RegularExpressions;

    006using System.Web;

    007namespace CLB.Utility.CharTools

    008{

    009 public static class StringHelper

    010 {

    011 /// <summary>

    012 /// 按字節(jié)數(shù)截取字符串(后面加省略號...)

    013 /// </summary>

    014 ///<param name="origStr"> 原始字符串</param>

    015 ///<param name="endIndex"> 提取前endIdex個字節(jié)</param>

    016 /// <returns></returns>

    017 public static string GetSubString(string origStr, int endIndex)

    018 {

    019 if (origStr == null || origStr.Length == 0 || endIndex < 0)

    020 return "";

    021 int bytesCount = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(origStr);

    022 if (bytesCount > endIndex)

    023 {

    024 int readyLength = 0;

    025 int byteLength;

    026 for (int i = 0; i < origStr.Length; i++)

    027 {

    028 byteLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(new char[] { origStr[i] });

    029 readyLength += byteLength;

    030 if (readyLength == endIndex)

    031 {

    032 origStr = origStr.Substring(0, i + 1) + "...";

    033 break;

    034 }

    035 else if (readyLength > endIndex)

    036 {

    037 origStr = origStr.Substring(0, i) + "...";

    038 break;

    039 }

    040 }

    041 }

    042 return origStr;

    043 }

    044 /// <summary>

    045 /// 按字節(jié)數(shù)截取字符串(不帶省略號)

    046 /// </summary>

    047 /// <param name="origStr"> 原始字符串</param>

    048 /// <param name="endIndex"> 提取前endIdex個字節(jié)</param>

    049 /// <returns></returns>

    050 public static string GetSub1String(string origStr, int endIndex)

    051 {

    052 if (origStr == null || origStr.Length == 0 || endIndex < 0)

    053 return "";

    054 int bytesCount = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(origStr);

    055 if (bytesCount > endIndex)

    056 {

    057 int readyLength = 0;

    058 int byteLength;

    059 for (int i = 0; i < origStr.Length; i++)

    060 {

    061 byteLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(new char[] { origStr[i] });

    062 readyLength += byteLength;

    063 if (readyLength == endIndex)

    064 {

    065 origStr = origStr.Substring(0, i + 1);

    066 break;

    067 }

    068 else if (readyLength > endIndex)

    069 {

    070 origStr = origStr.Substring(0, i);

    071 break;

    072 }

    073 }

    074 }

    075 return origStr;

    076 }

    077 /// <summary>

    078 /// SQL字符串過濾

    079 ///  </summary>

    080 /// <param name="Str"></param>

    081 /// <returns></returns>

    082 public static bool ProcessSqlStr(string Str)

    083 {

    084 bool ReturnValue = true;

    085 try

    086 {

    087 if (Str.Trim() != "")

    088 {

    089 string SqlStr ="exec|insert+|select+|delete|update|count|chr|mid|master+

    |truncate|char|declare|drop+|drop

    +table|creat+|create|*|iframe|script|";

    090 SqlStr +="exec+|insert|delete+|update+|count(|count+|chr+|+mid

    (|+mid+|+master+|truncate+

    |char+|+char(|declare

    +|drop+table|creat+table";

    091 string[] anySqlStr = SqlStr.Split('|');

    092 foreach (string ss in anySqlStr)

    093 {

    094 if (Str.ToLower().IndexOf(ss) >= 0)

    095 {

    096 ReturnValue = false;

    097 break;

    098 }

    099 }

    100 }

    101 }

    102 catch

    103 {

    104 ReturnValue = false;

    105 }

    106 return ReturnValue;

    107 }

    108 /// <summary>

    109 /// 檢測是否符合email格式

    110 /// </summary>

    111 /// <param name="strEmail"> 要判斷的email字符串</param>

    112 ///<returns 判斷結(jié)果</returns>

    113 public static bool IsValidEmail(string strEmail)

    114 {

    115 return Regex.IsMatch(strEmail, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

    116 }

    117 /// <summary>

    118 /// 檢測是否是正確的Url

    119 /// </summary>

    120 /// <param name="strUrl"> 要驗證的Url</param>

    121 /// <returns>判斷結(jié)果</returns>

    122 public static bool IsURL(string strUrl)

    123 {

    124 return Regex.IsMatch(strUrl, @"^(http|https)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(:[0-9]+)*(/($|[a-zA-Z0-9.,?'+&%$#=~_-]+))*$");

    125 }

    126 /// <summary>

    127 /// 檢測是否有Sql危險字符

    128 /// </summary>

    129 /// <param name="str"> 要判斷字符串</param>

    130 ///<returns> 判斷結(jié)果</returns>

    131 public static bool IsSafeSqlString(string str)

    132 {

    133 return !Regex.IsMatch(str, @"[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']");

    134 }

    135 /// <summary>

    136 /// 改正sql語句中的轉(zhuǎn)義字符

    137 /// </summary>

    138 public static string mashSQL(string str)

    139 {

    140 string str2;

    141 if (str == null)

    142 {

    143 str2 = "";

    144 }

    145 else

    146 {

    147 str = str.Replace("'", "'");

    148 str2 = str;

    149 }

    150 return str2;

    151 }

    152 /// <summary>

    153 /// 獲得當(dāng)前頁面客戶端的IP

    154 /// </summary>

    155 /// <returns>當(dāng)前頁面客戶端的IP</returns>

    156 public static string GetIP()

    157 {

    158 string result = String.Empty;

    159 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    160 if (null == result || result == String.Empty)

    161 {

    162 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

    163 }

    164 if (null == result || result == String.Empty)

    165 {

    166 result = HttpContext.Current.Request.UserHostAddress;

    167 }

    168 if (null == result || result == String.Empty || !IsIP(result))

    169 {

    170 return "0.0.0.0";

    171 }

    172 return result;

    173 }

    174 /// <summary>

    175 /// 是否為ip

    176 ///  </summary>

    177 /// <param name="ip"></param>

    178 /// <returns></returns>

    179 public static bool IsIP(string ip)

    180 {

    181 return Regex.IsMatch(ip, @"^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$");

    182 }

    183 /// <summary>

    184 /// 將全角數(shù)字轉(zhuǎn)換為數(shù)字

    185 /// </summary>

    186 /// <param name="SBCCase"></param>

    187 /// <returns></returns>

    188 public static string SBCCaseToNumberic(string SBCCase)

    189 {

    190 char[] c = SBCCase.ToCharArray();

    191 for (int i = 0; i < c.Length; i++)

    192 {

    193 byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);

    194 if (b.Length == 2)

    195 {

    196 if (b[1] == 255)

    197 {

    198 b[0] = (byte)(b[0] + 32);

    199 b[1] = 0;

    200 c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];

    201 }

    202 }

    203 }

    204 return new string(c);

    205 }

    206 /// <summary>

    207 /// 判斷某值是否在枚舉內(nèi)(位枚舉)

    208 /// </summary>

    209 ///<param name="checkingValue"> 被檢測的枚舉值</param>

    210 ///<param name="expectedValue"> 期望的枚舉值</param>

    211 /// <returns>是否包含</returns>

    212 public static bool CheckFlagsEnumEquals(Enum checkingValue, Enum expectedValue)

    213 {

    214 int intCheckingValue = Convert.ToInt32(checkingValue);

    215 int intExpectedValue = Convert.ToInt32(expectedValue);

    216 return (intCheckingValue & intExpectedValue) == intExpectedValue;

    217 }

    218 }

    219}

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:C#字符串工具類截取過濾格式判斷等
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)