C#字符串工具类截取过滤格式判断等_第1页
C#字符串工具类截取过滤格式判断等_第2页
C#字符串工具类截取过滤格式判断等_第3页
C#字符串工具类截取过滤格式判断等_第4页
C#字符串工具类截取过滤格式判断等_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#字符串工具类,实现的功能包括:判断某值是否在枚举内(位枚举)、将全角数字转换为数字、判断是否为IP、获得当前页面客户端的IP、改正sql语句中的转义字符、检测是否 是正确的Url、检测是否符合email格式、SQL字符串过滤、按字节数截取字符串(不带省略号)、按字节数截取字符串(后面加省略号)等。view sourceprint?001using System;002using System.Collections.Generic;003using System.Linq;004using System.Text;005using System.Text.RegularExpression

2、s;006using System.Web;007namespace CLB.Utility.CharTools008009 public static class StringHelper010 011/ <summary>012 /按字节数截取字符串(后面加省略号)013 / </summary>014 /<param name="origStr">原始字符串 </param>015 /<param name="endIndex&quo

3、t;> 提取前 endIdex 个字节 </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.Encod

4、ing.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口 origStri );

5、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 / <summa

6、ry>045 /按字节数截取字符串(不带省略号)046 / </summary>047 / <param name="origStr">原始字符串 </param>048 / <param name="endIndex"> 提取前 endIdex 个字节 </param>049 / <returns></returns>050 public sta

7、tic 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 readyLen

8、gth = 0;058 int byteLength;059 for (int i = 0; i < origStr.Length; i+)060 061 byteLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(new char口 origStri );062 readyLength += byteLength;063 if (readyLength = endIndex)064 065 origStr = origStr.Substring(0, i + 1);066 brea

9、k;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 / &

10、lt;returns></returns>082 public static bool ProcessSqlStr(string Str)083 084 bool ReturnValue = true;085 try086 087 if (Str.Trim() !="")088 089 string SqlStr ="exec|insert+|select+|delete|update|count|chr|mid|master+ |truncate|char|declare|drop+|drop +table|creat+

11、|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

12、(ss) >= 0)095 096 ReturnValue = false;097 break;098 099 100 101 102 catch103 104 ReturnValue = false;105 106 return ReturnValue;107 108 / <summary>109 /检测是否符合email格式110 / </summary>111 / <param name="strEmail"> 要判断的 email 字符串 </param&

13、amp;gt;112 /<returns 判断结果 </returns>113 public static bool IsValidEmail(string strEmail)114 115returnRegex.IsM10-92|1-910-91|1-9).(250-5|20-40-9|0-110-92|1-910- 91|1-90).(250-5|20-40-9|0-110-92|1-910-91|1-90).(250-5|20-40-9 “0-110-92|1-910-91|0-9)|localhost|(a-zA-Z0-9-+.)*a-zA-Z

14、0-9-+.(com|edu|go v|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|a-zA-Z1,10)(:0-9+)*(/($|a-zA-Z0-9.,?'+&%$#=_-+)*$");125 126 / <summary>127 /检测是否有Sql危险字符128 / </summary>129 / <param name="str">要判断字符串 </param&g

15、t;130 /<returns>判断结果 </returns>131 public static bool IsSafeSqlString(string str)132 133 return !Regex.IsMatch(str, "-|;|,|/|(|)|%|*|!口)134 135 / <summary>136 /改正sql语句中的转义字符137 / </summary>138 public static string mashSQL(string str)139 14

16、0 string str2;141 if (str = null)142 143 str2 =""144 145 else146 147 str = str.Replace(皿,皿);148 str2 = str;149 150 return str2;151 152 / <summary>153 /获得当前页面客户端的IP154 / </summary>155 / <returns>当前页面客户端的IP</returns>156 public static st

17、ring GetIP()157 158 string result = String.Empty;159resultHttpContext.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 = S

18、tring.Empty)165 166 result = HttpContext.Current.Request.UserHostAddress;167 168 if (null = result | result = String.Empty | !IsIP(result)169 170 return ""171 172 return result;173 174 / <summary>175 /是否为ip176 /  </summary>177 / <param nam

19、e="ip"></param>178 / <returns&gummary>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 (b1 = 255)197 198 b0 = (byte)(b0 + 32);199 b1 = 0;200 ci = System.Text.Encoding.Unicode.GetChars

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论