using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
namespace DataexchangeServer
{
///
/// ×Ö·û´®º¯Êý¸¨ÖúÀà¡£
///
public class StringHelper
{
///
/// SQL±í´ïʽ·Ö½â±í´ïʽÖеÄÌØÊâ²ÎÊý£¬°üÀ¨²ÎÊý¡¢¸¸¼Ç¼¡¢¼Ç¼¡¢È«¾Ö±äÁ¿µÈ¡£
/// Àý£º½«±í´ïʽ
/// Éú²úÊý>90 AND ¹¤ºÅ=para:P0123 OR DMA002=parent:DMA001 OR Ãû³Æ=field:D01 OR AA=@@01
/// ÖеÄÌØÊâ²ÎÊý²éÕÒ³öÀ´:para:P0123,parent:DMA001,field:D01,@@01
///
public const string Rggex_SqlExpression = @"\w+[><=]\w*((para|parent|field|@@)([:]*)+\w*)";
private static Regex RegexBr = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
///
/// µÃµ½ÕýÔò±àÒë²ÎÊýÉèÖÃ
///
///
public static RegexOptions GetRegexCompiledOptions()
{
#if NET1
return RegexOptions.Compiled;
#else
return RegexOptions.None;
#endif
}
///
/// ·µ»Ø×Ö·û´®ÕæÊµ³¤¶È, 1¸öºº×Ö³¤¶ÈΪ2¡£
///
///
public static int GetStringLength(string str)
{
return Encoding.Default.GetBytes(str).Length;
}
///
/// ÅжÏ×Ö·û´®µÄ°üº¬¹ØÏµ¡£
///
/// ±»°üº¬µÄ×Ö·û´®¡£
/// ÒÔÖ¸¶¨·Ö¸ô·ûÃèÊöµÄ×Ö·û´®Êý×é¡£
/// ×Ö·û´®Êý×éµÄ·Ö¸ô·û¡£
/// Èç¹û±»°üº¬µÄ×Ö·û´®ÔÚÖ¸¶¨×Ö·û´®Êý×éÖУ¬Ôò·µ»Øtrue,·ñÔò·µ»Øfalse¡£
public static bool IsCompriseString(string str, string stringArray, string stringSplit)
{
if (stringArray == "" || stringArray == null)
{
return false;
}
str = str.ToLower();
string[] array = SafeSplitString(stringArray.ToLower(), stringSplit);
for (int i = 0; i < stringArray.Length; i++)
{
//string t1 = str;
//string t2 = stringArray[i];
if (str.IndexOf(array[i]) > -1)
{
return true;
}
}
return false;
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÔÚÖ¸¶¨×Ö·û´®Êý×éÖеÄλÖá£
///
/// ´ýËÑË÷µÄ×Ö·û´®¡£
/// ×Ö·û´®Êý×é¡£
/// ÊÇ·ñ²»Çø·Ö´óСд, trueΪ²»Çø·Ö, falseÎªÇø·Ö¡£
/// ×Ö·û´®ÔÚÖ¸¶¨×Ö·û´®Êý×éÖеÄλÖÃ, Èç²»´æÔÚÔò·µ»Ø-1¡£
public static int GetIndexInArray(string strSearch, string[] stringArray, bool caseInsensetive)
{
for (int i = 0; i < stringArray.Length; i++)
{
if (caseInsensetive)
{
if (strSearch.ToLower() == stringArray[i].ToLower())
{
return i;
}
}
else
{
if (strSearch == stringArray[i])
{
return i;
}
}
}
return -1;
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÔÚÖ¸¶¨×Ö·û´®Êý×éÖеÄλÖá£
///
/// ×Ö·û´®¡£
/// ×Ö·û´®Êý×é¡£
/// ×Ö·û´®ÔÚÖ¸¶¨×Ö·û´®Êý×éÖеÄλÖÃ, Èç²»´æÔÚÔò·µ»Ø-1¡£
public static int GetIndexInArray(string strSearch, string[] stringArray)
{
return GetIndexInArray(strSearch, stringArray, true);
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÊÇ·ñÊôÓÚÖ¸¶¨×Ö·û´®Êý×éÖеÄÒ»¸öÔªËØ¡£
///
/// ´ýËÑË÷µÄ×Ö·û´®¡£
/// ×Ö·û´®Êý×é¡£
/// ÊÇ·ñ²»Çø·Ö´óСд, trueΪ²»Çø·Ö, falseÎªÇø·Ö¡£
/// ÅжϽá¹û¡£
public static bool IsInArray(string strSearch, string[] stringArray, bool caseInsensetive)
{
return GetIndexInArray(strSearch, stringArray, caseInsensetive) >= 0;
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÊÇ·ñÊôÓÚÖ¸¶¨×Ö·û´®Êý×éÖеÄÒ»¸öÔªËØ¡£
///
/// ´ýËÑË÷µÄ×Ö·û´®¡£
/// ×Ö·û´®Êý×é¡£
/// ÅжϽá¹û¡£
public static bool IsInArray(string str, string[] stringArray)
{
return IsInArray(str, stringArray, false);
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÊÇ·ñÊôÓÚÖ¸¶¨×Ö·û´®Êý×éÖеÄÒ»¸öÔªËØ¡£
///
/// ´ýËÑË÷µÄ×Ö·û´®¡£
/// ÄÚ²¿ÒԷֺŷָ´ÊµÄ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsInArray(string str, string stringArray)
{
return IsInArray(str, SafeSplitString(stringArray, ";"), false);
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÊÇ·ñÊôÓÚÖ¸¶¨×Ö·û´®Êý×éÖеÄÒ»¸öÔªËØ¡£
///
/// ´ýËÑË÷µÄ×Ö·û´®
/// ÄÚ²¿ÒÔÖ¸¶¨·Ö¸î·û·Ö¸îµ¥´ÊµÄ×Ö·û´®¡£
/// ·Ö¸î×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsInArray(string str, string stringArray, string stringSplit)
{
return IsInArray(str, SafeSplitString(stringArray, stringSplit), false);
}
///
/// ÅжÏÖ¸¶¨×Ö·û´®ÊÇ·ñÊôÓÚÖ¸¶¨×Ö·û´®Êý×éÖеÄÒ»¸öÔªËØ¡£
///
/// ´ýËÑË÷µÄ×Ö·û´®
/// ÄÚ²¿ÒÔÖ¸¶¨·Ö¸î·û·Ö¸îµ¥´ÊµÄ×Ö·û´®¡£
/// ·Ö¸î×Ö·û´®¡£
/// ÊÇ·ñ²»Çø·Ö´óСд, trueΪ²»Çø·Ö, falseÎªÇø·Ö¡£
/// ÅжϽá¹û¡£
public static bool IsInArray(string str, string stringArray, string stringSplit, bool caseInsensetive)
{
return IsInArray(str, SafeSplitString(stringArray, stringSplit), caseInsensetive);
}
///
/// ɾ³ý×Ö·û´®Î²²¿µÄ»Ø³µ/»»ÐÐ/¿Õ¸ñ·ûºÅ¡£
///
/// ´ý´¦ÀíµÄ×Ö·û´®¡£
/// ´¦ÀíºóµÄ×Ö·û´®¡£
public static string RTrim(string str)
{
for (int i = str.Length; i >= 0; i--)
{
if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
{
str.Remove(i, 1);
}
}
return str;
}
///
/// Çå³ý¸ø¶¨×Ö·û´®ÖеĻسµ¼°»»Ðзû¡£
///
/// ÒªÇå³ýµÄ×Ö·û´®¡£
/// ´¦ÀíºóµÄ×Ö·û´®¡£
public static string SafeClearBR(string str)
{
Match m = null;
for (m = RegexBr.Match(str); m.Success; m = m.NextMatch())
{
str = str.Replace(m.Groups[0].ToString(), "");
}
return str;
}
///
/// ´Ó×Ö·û´®µÄÖ¸¶¨Î»ÖýØÈ¡Ö¸¶¨³¤¶ÈµÄ×Ó×Ö·û´®¡£
///
/// Ô×Ö·û´®¡£
/// ×Ó×Ö·û´®µÄÆðʼλÖá£
/// ×Ó×Ö·û´®µÄ³¤¶È¡£
/// ×Ó×Ö·û´®¡£
public static string SafeCutString(string str, int startIndex, int length)
{
if (startIndex >= 0)
{
if (length < 0)
{
length = length * -1;
if (startIndex - length < 0)
{
length = startIndex;
startIndex = 0;
}
else
{
startIndex = startIndex - length;
}
}
if (startIndex > str.Length)
{
return "";
}
}
else
{
if (length < 0)
{
return "";
}
else
{
if (length + startIndex > 0)
{
length = length + startIndex;
startIndex = 0;
}
else
{
return "";
}
}
}
if (str.Length - startIndex < length)
{
length = str.Length - startIndex;
}
return str.Substring(startIndex, length);
}
///
/// ´Ó×Ö·û´®µÄÖ¸¶¨Î»ÖÿªÊ¼½ØÈ¡µ½×Ö·û´®½áβµÄÁË·û´®¡£
///
/// Ô×Ö·û´®¡£
/// ×Ó×Ö·û´®µÄÆðʼλÖá£
/// ×Ó×Ö·û´®¡£
public static string SafeCutString(string str, int startIndex)
{
return SafeCutString(str, startIndex, str.Length);
}
///
/// intÐÍת»»ÎªstringÐÍ¡£
///
/// ת»»ºóµÄstringÀàÐͽá¹û¡£
public static string FromInt(int intValue)
{
//
return Convert.ToString(intValue);
}
///
/// ȡָ¶¨³¤¶ÈµÄ×Ö·û´®£¬²¢ÔÚ½ØÈ¡Ê±¿¼ÂÇË«Óï×Ö·û£¬µ±×Ö·û´®Èç¹û²Ù¹ýÖ¸¶¨³¤¶ÈÔò½«³¬³öµÄ²¿·ÖÓÃÖ¸¶¨×Ö·û´®´úÌæ¡£
///
/// Òª¼ì²éµÄ×Ö·û´®¡£
/// Ö¸¶¨³¤¶È¡£
/// ÓÃÓÚÌæ»»µÄ×Ö·û´®¡£
/// ½ØÈ¡ºóµÄ×Ö·û´®¡£
public static string GetSubString(string srcString, int length, string tailString)
{
return GetSubString(srcString, 0, length, tailString);
}
///
/// ȡָ¶¨³¤¶ÈµÄ×Ö·û´®£¬²¢ÔÚ½ØÈ¡Ê±¿¼ÂÇË«Óï×Ö·û£¬µ±×Ö·û´®Èç¹û²Ù¹ýÖ¸¶¨³¤¶ÈÔò½«³¬³öµÄ²¿·ÖÓÃÖ¸¶¨×Ö·û´®´úÌæ¡£
///
/// Òª¼ì²éµÄ×Ö·û´®¡£
/// ÆðʼλÖá£
/// Ö¸¶¨³¤¶È¡£
/// ÓÃÓÚÌæ»»µÄ×Ö·û´®¡£
/// ½ØÈ¡ºóµÄ×Ö·û´®¡£
public static string GetSubString(string srcString, int startIndex, int length, string tailString)
{
string myResult = srcString;
//µ±ÊÇÈÕÎÄ»òº«ÎÄʱ(×¢:ÖÐÎĵķ¶Î§:\u4e00 - \u9fa5, ÈÕÎÄÔÚ\u0800 - \u4e00, º«ÎÄΪ\xAC00-\xD7A3)
if (System.Text.RegularExpressions.Regex.IsMatch(srcString, "[\u0800-\u4e00]+") ||
System.Text.RegularExpressions.Regex.IsMatch(srcString, "[\xAC00-\xD7A3]+"))
{
//µ±½ØÈ¡µÄÆðʼλÖó¬³ö×ֶδ®³¤¶Èʱ
if (startIndex >= srcString.Length)
{
return "";
}
else
{
return srcString.Substring(startIndex,
((length + startIndex) > srcString.Length) ? (srcString.Length - startIndex) : length);
}
}
if (length >= 0)
{
byte[] bsSrcString = Encoding.Default.GetBytes(srcString);
//µ±×Ö·û´®³¤¶È´óÓÚÆðʼλÖÃ
if (bsSrcString.Length > startIndex)
{
int endIndex = bsSrcString.Length;
//µ±Òª½ØÈ¡µÄ³¤¶ÈÔÚ×Ö·û´®µÄÓÐЧ³¤¶È·¶Î§ÄÚ
if (bsSrcString.Length > (startIndex + length))
{
endIndex = length + startIndex;
}
else
{ //µ±²»ÔÚÓÐЧ·¶Î§ÄÚʱ,ֻȡµ½×Ö·û´®µÄ½áβ
length = bsSrcString.Length - startIndex;
tailString = "";
}
int nRealLength = length;
int[] anResultFlag = new int[length];
byte[] bsResult = null;
int nFlag = 0;
for (int i = startIndex; i < endIndex; i++)
{
if (bsSrcString[i] > 127)
{
nFlag++;
if (nFlag == 3)
{
nFlag = 1;
}
}
else
{
nFlag = 0;
}
anResultFlag[i] = nFlag;
}
if ((bsSrcString[endIndex - 1] > 127) && (anResultFlag[length - 1] == 1))
{
nRealLength = length + 1;
}
bsResult = new byte[nRealLength];
Array.Copy(bsSrcString, startIndex, bsResult, 0, nRealLength);
myResult = Encoding.Default.GetString(bsResult);
myResult = myResult + tailString;
}
}
return myResult;
}
///
/// ×Ô¶¨ÒåµÄÌæ»»×Ö·û´®º¯Êý¡£
///
/// Ô´×Ö·û´®¡£
/// ´ýÌæ»»µÄ×Ö·û´®
/// Ìæ»»ºóµÄ×Ö·û´®
/// ÊÇ·ñ²»Çø·Ö´óСд, trueΪ²»Çø·Ö, falseÎªÇø·Ö¡£
/// ´¦ÀíºóµÄ×Ö·û´®¡£
public static string SafeReplaceString(string sourceString, string searchString, string replaceString, bool caseInsensetive)
{
return Regex.Replace(sourceString, Regex.Escape(searchString), replaceString, caseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
}
///
/// ¼ì²âÊÇ·ñ·ûºÏemailµØÖ·¸ñʽ¡£
///
/// ÒªÅжϵÄemail×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsValidEmail(string strEmail)
{
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})(\]?)$");
}
///
/// ¼ì²âÊÇ·ñ·ûºÏemailÖ·ÖÐÓòÃû¸ñʽ¡£
///
/// ÒªÅжϵÄemail×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsValidEmailDomain(string strEmail)
{
return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
///
/// ¼ì²âÊÇ·ñ·ûºÏUrl¸ñʽ¡£
///
/// ÒªÅжϵÄUrl¡£
/// ÅжϽá¹û¡£
public static bool IsUrl(string strUrl)
{
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\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
///
/// ¼ì²âÊÇ·ñΪbase64×Ö·û´®¡£
///
/// ÒªÅжϵÄ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsBase64String(string str)
{
//A-Z, a-z, 0-9, +, /, =
return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
}
///
/// ¼ì²âÊÇ·ñÓÐSqlΣÏÕ×Ö·û¡£
///
/// ÒªÅжÏ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
///
/// ¼ì²âÊÇ·ñÓÐΣÏյĿÉÄÜÓÃÓÚÁ´½ÓµÄ×Ö·û´®¡£
///
/// ÒªÅжÏ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsSafeUserInfoString(string str)
{
return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|ÓοÍ|^Guest");
}
///
/// ÇåÀí×Ö·û´®¡£
///
/// ÊäÈëµÄ×Ö·û´®¡£
/// ´¦Àí½á¹û
public static string SafeCleanInput(string input)
{
return Regex.Replace(input.Trim(), @"[^\w\.@-]", "");
}
///
/// ¼ì²âÊÇ·ñΪʱ¼ä×Ö·û´®¡£
///
/// ´ýÅжϵÄʱ¼ä×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsTime(string str)
{
return Regex.IsMatch(str, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
}
/*
///
/// ½«×Ö·û´®×ª»»Îª¼òÌåÖÐÎÄ¡£
/// (([^"']|^)\[xui:+\w+\.+\w+\.+\w+\])+(\.+\w*)+(\[\d\])?
/// [xui:form1.bysv.ss].value[102]=="[xui:form1.bysv.ss]" and [xui:form2.bysv.ss].value==c or [ps:0.1]==d
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ת»»½á¹û¡£
public static string ToSimplifiedChinese(string str)
{
return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);
}
///
/// ½«×Ö·û´®×ª»»Îª·±ÌåÖÐÎÄ¡£
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ת»»½á¹û¡£
public static string ToTChinese(string str)
{
return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
}
* */
///
/// ·Ö¸î×Ö·û´®¡£
///
/// ´ý·Ö¸îµÄÔ´×Ö·û´®¡£
/// ·Ö¸î·û¡£
/// ·Ö¸îºóµÄ×Ö·û´®Êý×é¡£
public static string[] SafeSplitString(string srcString, string stringSplit)
{
if (srcString.IndexOf(stringSplit) < 0)
{
string[] tmp = { srcString };
return tmp;
}
return Regex.Split(srcString, Regex.Escape(stringSplit), RegexOptions.IgnoreCase);
}
///
/// ·Ö¸î×Ö·û´®¡£
///
/// ´ý·Ö¸îµÄÔ´×Ö·û´®¡£
/// ·Ö¸î·û¡£
/// ·µ»Ø×Ö·û´®Êý¾ÝµÄ³¤¶È£¬Èç¹û²»×ãÔòÒÔ¿Õ×Ö·û´®Ìî³ä¡£
/// ·Ö¸îºóµÄ×Ö·û´®Êý×é¡£
public static string[] SafeSplitString(string srcString, string stringSplit, int length)
{
string[] result = new string[length];
string[] splited = SafeSplitString(srcString, stringSplit);
for (int i = 0; i < length; i++)
{
if (i < splited.Length)
result[i] = splited[i];
else
result[i] = string.Empty;
}
return result;
}
///
/// ʹÓÃÕýÔò±í´ïʽƥÅä×Ö·û´®£¬ÕÒ³öÿ¸ö·ûºÏÌõ¼þµÄµÚÒ»×éÊý¾Ý¡£
///
///
///
///
/// ͨ³£ÓÃÓÚÌæ»»±í´ïʽÖеÄÌØÊâ±ê¼Ç¡£
public static string[] SafeMatchString(string regx, string source)
{
Regex r = new Regex(regx, RegexOptions.IgnoreCase);
Match m = r.Match(source);
List s = new List();
while (m.Success)
{
s.Add(m.Groups[1].Value);
m = m.NextMatch();
}
return s.ToArray();
}
///
/// ½øÐÐÖ¸¶¨µÄÌæ»»(Ôà×Ö¹ýÂË)¡£
///
/// ´ý´¦ÀíµÄ×Ö·û´®¡£
/// ´ý¹ýÂ˵Ä×Ö·û´®£¨Ôà×Ö£©¡£
/// ´¦Àí½á¹û¡£
public static string Filter(string str, string bantext)
{
string text1 = "";
string text2 = "";
string[] textArray1 = SafeSplitString(bantext, "\r\n");
for (int num1 = 0; num1 < textArray1.Length; num1++)
{
text1 = textArray1[num1].Substring(0, textArray1[num1].IndexOf("="));
text2 = textArray1[num1].Substring(textArray1[num1].IndexOf("=") + 1);
str = str.Replace(text1, text2);
}
return str;
}
///
/// ¼ÆËã×Ö½ÚÊý´óС²¢·µ»Ø×îÆ¥Å䵥λµÄÏàÓ¦µÄ×Ö·û´®¡£
///
/// ×Ö½Ú´óС¡£
/// ×îÆ¥Å䵥λµÄÏàÓ¦µÄ×Ö·û´®¡£
public static string FormatBytesString(int size)
{
if (size > 1073741824)
{
return ((double)(size / 1073741824)).ToString("0") + "G";
}
if (size > 1048576)
{
return ((double)(size / 1048576)).ToString("0") + "M";
}
if (size > 1024)
{
return ((double)(size / 1024)).ToString("0") + "K";
}
return size.ToString() + "Bytes";
}
///
/// ¼ì²âÊÇ·ñ·ûºÏIPµØÖ·¸ñʽ(IPv4)¡£
///
/// ÒªÅжϵÄIP×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsIP(string strIp)
{
return Regex.IsMatch(strIp, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
///
/// ¼ì²âÊÇ·ñÊÇIPµØÖ·¶Î(IPv4)¡£
///
/// ÒªÅжϵÄIP×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsIPSect(string strIp)
{
return Regex.IsMatch(strIp, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");
}
///
/// ½«È«½ÇÊý×Öת»»ÎªÊý×Ö¡£
///
/// È«½ÇÊý×Ö¡£
/// ´¦Àí½á¹û¡£
public static string SBCCaseToNumberic(string SBCCase)
{
char[] c = SBCCase.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 255)
{
b[0] = (byte)(b[0] + 32);
b[1] = 0;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
return new string(c);
}
///
/// ɾ³ý×îºóÒ»¸ö×Ö·û¡£
///
/// ´ý´¦ÀíµÄ×Ö·û´®¡£
/// ´¦Àí½á¹û¡£
public static string SafeClearLastChar(string str)
{
if (str == "")
return "";
else
return str.Substring(0, str.Length - 1);
}
///
/// ½«×Ö·û´®×ª»»ÎªColor¡£
///
/// ÑÕÉ«×Ö·û´®£¬ÑÕÉ«¿ÉÒÔÊÇÑÕÉ«Ãû£¬Ò²¿ÉÒÔÊÇRGBÖµ»ò±ê×¼ÑÕɫֵ¡£
/// ´¦Àí½á¹û¡£
public static Color ToColor(string color)
{
int red, green, blue = 0;
char[] rgb;
color = color.TrimStart('#');
color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
switch (color.Length)
{
case 3:
rgb = color.ToCharArray();
red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
return Color.FromArgb(red, green, blue);
case 6:
rgb = color.ToCharArray();
red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
return Color.FromArgb(red, green, blue);
default:
return Color.FromName(color);
}
}
///
/// ¼ì²â×Ö·û´®ÊÇ·ñÊÇÈÕÆÚ×Ö·û´®¡£
///
/// ´ýÅжÏ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsDateString(string str)
{
return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
}
///
/// ¼ì²âÊÇ·ñΪÕýÕûÊý¡£
///
/// ´ýÅжÏ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsPlusInt(string str)
{
return Regex.IsMatch(str, @"^[0-9]*$");
}
///
/// ¼ì²â¸ø¶¨µÄ×Ö·û´®Êý×é(strNumber)ÖеÄÊý¾ÝÊDz»ÊǶ¼ÎªÊýÖµÐÍ¡£
///
/// ҪȷÈϵÄ×Ö·û´®Êý×é¡£
/// ÅжϽá¹û¡£
public static bool IsNumericArray(string[] strNumber)
{
if (strNumber == null)
{
return false;
}
if (strNumber.Length < 1)
{
return false;
}
foreach (string id in strNumber)
{
if (!IsNumeric(id))
{
return false;
}
}
return true;
}
///
/// ¼ì²â×Ö·û´®ÊÇ·ñΪInt32ÀàÐ͵ÄÊý×Ö¡£
///
/// ´ýÅжϵÄ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsNumeric(string str)
{
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
{
return true;
}
}
return false;
}
///
/// ¼ì²â×Ö·û´®ÊÇ·ñΪDoubleÀàÐ͵ÄÊý×Ö¡£
///
/// ´ýÅжϵÄ×Ö·û´®¡£
/// ÅжϽá¹û¡£
public static bool IsDouble(string str)
{
return Regex.IsMatch(str, @"^([0-9])[0-9]*(\.\w*)?$");
}
///
/// stringÐÍת»»ÎªboolÐÍ¡£
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ȱʡֵ¡£
/// ת»»ºóµÄboolÀàÐͽá¹û¡£
public static bool ToBool(string str, bool defaultValue)
{
if (str != null)
{
if (string.Compare(str, "true", true) == 0)
{
return true;
}
else if (string.Compare(str, "false", true) == 0)
{
return false;
}
}
return defaultValue;
}
///
/// ½«×Ö·û´®×ª»»ÎªInt32ÀàÐÍ¡£
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ȱʡֵ¡£
/// ת»»ºóµÄintÀàÐͽá¹û¡£
public static int ToInt(string str, int defaultValue)
{
if (str != null)
{
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
{
return Convert.ToInt32(str);
}
}
}
return defaultValue;
}
///
/// stringÐÍת»»ÎªfloatÐÍ¡£
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ȱʡֵ¡£
/// ת»»ºóµÄintÀàÐͽá¹û¡£
public static float ToFloat(string str, float defaultValue)
{
if ((str == null) || (str.Length > 10))
{
return defaultValue;
}
float intValue = defaultValue;
if (str != null)
{
bool IsFloat = Regex.IsMatch(str.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
{
intValue = Convert.ToSingle(str);
}
}
return intValue;
}
///
/// ½«×Ö·û´®×ª»»ÎªInt32ÀàÐÍ
///
/// ´ýת»»µÄ×Ö·û´®¡£
/// ת»»ºóµÄInt32ÀàÐͽá¹û¡£
public static int ToInt32(string str)
{
if (str == null)
{
return 0;
}
string strNum = str;
if (IsNumeric(strNum))
{
if (strNum.ToString().Length > 9)
{
if (strNum.StartsWith("-"))
{
return int.MinValue;
}
else
{
return int.MaxValue;
}
}
return Int32.Parse(strNum);
}
else
{
return 0;
}
}
///
/// ½«×Ö·û´®×ª»»ÎªBase64ÀàÐÍ
///
///
///
public static string ToBase64String(string str)
{
byte[] data = System.Text.Encoding.Unicode.GetBytes(str);
return Convert.ToBase64String(data);
}
///
/// FromBase64String
///
///
///
public static string FromBase64String(string str)
{
byte[] data = Convert.FromBase64String(str);
return System.Text.Encoding.Unicode.GetString(data);
}
///
/// ѹËõ×Ö·û´®
///
///
///
public static string ToCompressionString(string str)
{
return str;
}
///
/// ½âѹ×Ö·û´®
///
///
///
public static string FromCompressionString(string str)
{
return str;
}
///
/// °Ñ×Ö·û´®Êý¾Ýת»¯Îª×Ö·û
///
///
///
///
public static string FromStringArray(string[] array, string separator)
{
string s = string.Empty;
foreach (string t in array)
s += separator + t;
if (s.Length > separator.Length)
s = s.Substring(separator.Length);
return s;
}
///
/// ½«×Öµäת»»Îª×Ö·û´®±í´ïʽ¡£
///
///
///
public static string FromDictionary(Dictionary dic)
{
if (dic.Count == 0)
return string.Empty;
string rstr = string.Empty;
foreach (string key in dic.Keys)
{
rstr += ";" + key + "=" + dic[key];
}
if (rstr.Length > 1)
rstr = rstr.Substring(1);
return rstr;
}
///
/// ½«Êý×éת»»Îª×Ö·û´®±í´ïʽ¡£
///
///
/// ÊÇ·ñת»¯Îª×ÖµäÑùʽ
///
public static string FromArray(string[] array,bool dictionary)
{
if (array.Length == 0)
return string.Empty;
string rstr = string.Empty;
foreach (string key in array)
{
if(dictionary)
rstr += ";" + key + "=" + key;
else
rstr += ";" + key ;
}
if (rstr.Length > 1)
rstr = rstr.Substring(1);
return rstr;
}
///
/// ½«×Ö·û´®½âÎöΪ×ֵ䣬ҪÇó×Ö·û´®µÄ¸ñʽΪ:¼ü=Öµ;¼ü=Öµ
///
/// ×Ö·û´®¡£
///
public static Dictionary ToDictionary(string str)
{
Dictionary dic = new Dictionary();
if (str == null || str == string.Empty)
return dic;
string[] items = str.Split(new char[1] { ';' });
for (int i = 0; i <= items.Length - 1; i++)
{
if (!items[i].Contains("="))
continue;
string[] item = items[i].Split(new char[1] { '=' });
dic.Add(item[0], item[1]);
}
return dic;
}
/*
public static string FromNamedValueCollection(NamedValueItemCollection dic)
{
if (dic.Count == 0)
return string.Empty;
string rstr = string.Empty;
foreach (NamedValueItem item in dic)
{
rstr += ";" + item.Name + "=" + item.StringValue;
}
if (rstr.Length > 1)
rstr = rstr.Substring(1);
return rstr;
}
public static NamedValueItemCollection ToNamedValueCollection(string str)
{
NamedValueItemCollection dic = new NamedValueItemCollection();
if (str == null || str == string.Empty)
return dic;
string[] items = str.Split(new char[1] { ';' });
for (int i = 0; i <= items.Length - 1; i++)
{
if (!items[i].Contains("="))
continue;
string[] item = items[i].Split(new char[1] { '=' });
dic.Add(item[0], item[1]);
}
return dic;
}
*/
///
/// ±í´ïʽֵ½»»»£¬¼´½«Öµ±í´ïʽÖеÄÖµ°´Ãû³ÆÌæ»»µ½Ô´±í´ïʽÖС£
///
/// Ô´±í´ïʽ,¸ñʽΪ Ãû³Æ=Öµ±í´ïʽÃû³Æ£»Ãû³Æ=Öµ±í´ïʽÃû³Æ
/// Öµ±í´ïʽ,¸ñʽΪֵ±í´ïʽÃû³Æ=Öµ;¸ñʽΪֵ±í´ïʽÃû³Æ=Öµ
/// ·µ»Øºó±í´ïʽΪ Ãû³Æ=Öµ;Ãû³Æ=Öµ
public static string ExpressionValueExchange(string srcExpr, string valExpr)
{
string rstr = string.Empty;
string[] src = srcExpr.Split(new char[1] { ';' });
string[] val = valExpr.Split(new char[1] { ';' });
Dictionary dval = new Dictionary();
for (int i = 0; i <= val.Length - 1; i++)
{
int p = val[i].IndexOf("=");
dval.Add(val[i].Substring(0, p), val[i].Substring(p + 1));
}
for (int i = 0; i <= src.Length - 1; i++)
{
int p = src[i].IndexOf("=");
string sn = src[i].Substring(0, p);
string sv = src[i].Substring(p+1);
if (dval.ContainsKey(sv))
sv = dval[sv];
else
sv = string.Empty;
if (rstr == string.Empty)
rstr = sn + "=" + sv;
else
rstr = rstr+";"+sn + "=" + sv;
}
return rstr;
}
///
/// ¸ñʽ»¯×Ö·û´®¡£
///
/// ¸ñʽ»¯×Ö·û´®£¬Öм䲻ÄܰüÀ¨¶ººÅ¡£¸ñʽΪ£º¸ñʽ»¯×Ö·û´®,²ÎÊý×Ö¶ÎÃû1,,²ÎÊý×Ö¶ÎÃû2
///
///
public static string FormatString(string formatText, DataRow parameters)
{
if (parameters == null)
return formatText;
string[] fsParts = formatText.Split(new char[1] { ',' });
string[] fparas = new string[fsParts.Length - 1];
for (int i = 1; i <= fsParts.Length - 1; i++)
{
if (parameters.Table.Columns.Contains(fsParts[i]))
fparas[i - 1] = parameters[fsParts[i]].ToString();
else
fparas[i - 1] = string.Empty;
}
string s = string.Format(fsParts[0], fparas);
return s;
}
///
/// »ñÈ¡·Ö¸ô×Ö·û´®Êý×é;È罫"[DBB004]=DAB004;[DBB005]=DAB005;[DBB006]=[DAB009]"·Ö¸ô×Ö·ûÊý×é:DBB004,DAB004,DBB005,DAB005,DBB006,DAB009
///
/// Ó³Éä×Ö·û
/// ×Ö·û´®Êý×é
public static string[] GetSplitString(string strValue)
{
if (strValue == null)
return null;
if (strValue.Length == 0)
return null;
strValue = strValue.Replace("[", "").Replace("]", "");
string[] strArray = strValue.Split(new char[2] { ';', '=' });
return strArray;
}
///
/// ¶ÔÎı¾,·Ö¸ôµÄ×Ö·û½øÐÐÌí¼Óµ¥ÒýºÅ,¶ÔÓÚÌõ¼þÊÇinµÄʱºò½øÐÐÌí¼Óµ¥ÒýºÅ
/// x022,x023,x025===>'x022','x023','x025'
///
/// ´«ÈëµÄ×Ö·û´®
/// ·µ»ØÌí¼Óµ¥ÒýºÅµÄ×Ö·û
public static string AddSingleQuote(string strText)
{
string[] value = strText.Split(new char[1] { ',' });
for (int i = 0; i < value.Length; i++)
{
string para = value[i];
if (para.StartsWith("'"))
para = para.Substring(1);
if (para.EndsWith("'"))
para = para.Substring(0, para.Length - 1);
value[i] = "'" + para + "'";
}
string str = string.Join(",", value);
return str;
}
///
/// Gets the express string.
///
/// The STR value.
///
public static string[] GetExpressString(string strValue)
{
string[] strs = strValue.Split(new char[2] { '=', ' ' });
List strList = new List();
foreach (string str in strs)
{
if (str.Contains("["))
strList.Add(str);
}
return strList.ToArray();
//return strs.Where(c => c.Contains('[')).ToArray();
}
///
/// ¼ìÑéÁ½¸öÊý×ÖÊÇ·ñÏàµÈ
///
/// µÚÒ»¸öÊý×Ö
/// µÚ¶þ¸öÊý×Ö
/// ·µ»Øtrue£»false
public static bool AboutEqual(double x, double y)
{
double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
return Math.Abs(x - y) <= epsilon;
}
///
/// ¼ìÑéÊý×ÖÊý×éÊÇ·ñÈ«²¿ÏàµÈ
/// ²ÉÓÃÏàµÈ´«µÝµÄ·½·¨£¬ÑéÖ¤Ò»ÂÖ¼´¿É
///
///
///
public static bool AboutEqual(params double[] douArrs)
{
double dbFirst = douArrs[0];
for (int t = 1; t < douArrs.Length; t++)
{
if (!AboutEqual(dbFirst, douArrs[t]))
return false;
}
return true;
}
///
/// ×Ö·ûÊý×éÑéÖ¤ÊÇ·ñÓÐΪ¿Õ»òÕßnull
///
///
///
public static bool ExistsEmptyOrNull(params object[] objs)
{
if (objs == null || objs.Length == 0)
return true;
foreach (object obj in objs)
{
if (IsNullOrEmptyReturnZero(obj) == "")
return true;
}
return false;
}
///
/// ·ºÐÍת»»ÎªÊýÖµÀàÐÍ
///
///
///
///
public static T IsNullOrEmptyReturnZero(object obj)
{
if (Equals(obj, null) || Equals(DBNull.Value, obj))
{
if (typeof(T).FullName == "System.String")
return (T)Convert.ChangeType("", typeof(T));
return default(T);
}
try
{
return (T)Convert.ChangeType(obj, typeof(T));
}
catch
{
return default(T);
}
}
}
}