您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

C#字符串工具类 截取、过滤、格式判断等

作者:佚名    责任编辑:admin    更新时间:2022-06-22

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

  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 /// 按字节数截取字符串(后面加省略号...)

  013 /// </summary>

 

 

 

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

  015 ///<param name="endIndex"> 提取前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.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 /// 按字节数截取字符串(不带省略号)

  046 /// </summary>

 

 

 

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

  048 /// <param name="endIndex"> 提取前endIdex个字节</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 判断结果</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>判断结果</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> 判断结果</returns>

  131 public static bool IsSafeSqlString(string str)

  132 {

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

  134 }

  135 /// <summary>

 

 

 

  136 /// 改正sql语句中的转义字