RaUI/Source/MyDb/DbManage/Strings.cs

402 lines
13 KiB
C#
Raw Normal View History

//--------------------------日期:2013-8-28
//--------------------------版本:2.0.1.0
//--------------------------作者:itrycn
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
//字符串操作类
namespace ryCommon
{
/// <summary>
/// 字符串操作类
/// </summary>
public class Strings
{
/// <summary>
/// 字符串转Base64
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public string StrToBase64(string input)
{
System.Text.Encoding encode = System.Text.Encoding.Default;
byte[] bytedata = encode.GetBytes(input);
return Convert.ToBase64String(bytedata, 0, bytedata.Length);
}
/// <summary>
/// Base64转字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public string Base64ToStr(string input)
{
try
{
byte[] bpath = Convert.FromBase64String(input);
return System.Text.Encoding.Default.GetString(bpath);
}
catch { return ""; }
}
/// <summary>
/// 转换指定字符串为布尔类型
/// </summary>
public bool StrToBool(string Str,bool defValue)
{
try
{
if (Str == "1" || Str.ToLower()=="true")
{
return true;
}
else
{
return false;
}
}
catch { return defValue; }
}
/// <summary>
/// 转换指定字符串为Double类型
/// </summary>
public double StrToDouble(string Str, double defValue)
{
try
{
return Convert.ToDouble(Str);
}
catch { return defValue; }
}
/// <summary>
/// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
/// </summary>
/// <param name="DateTime1">第一个日期和时间</param>
/// <param name="DateTime2">第二个日期和时间</param>
/// <returns></returns>
public string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = "";
try
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
if(ts.Days!=0)
{
dateDiff = ts.Days.ToString() + "天";
}
if (ts.Hours != 0)
{
dateDiff += ts.Hours.ToString() + "小时";
}
if (ts.Minutes != 0)
{
dateDiff += ts.Minutes.ToString() + "分钟";
}
if (ts.Seconds != 0)
{
dateDiff += ts.Seconds.ToString() + "秒";
}
}
catch
{
}
if (dateDiff == "") { dateDiff = "0秒"; }
return dateDiff;
}
/// <summary>
/// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值.
/// </summary>
/// <param name="DateTime1">一个日期和时间</param>
/// <returns></returns>
public string DateDiff(DateTime DateTime1)
{
return this.DateDiff(DateTime1, DateTime.Now);
}
/// <summary>
/// 转换指定字符串为Int类型
/// </summary>
public int StrToInt(string Str, int defValue)
{
try
{
return Convert.ToInt32(Str);
}
catch { return defValue; }
}
/// <summary>
/// 转换指定字符串为Int类型
/// </summary>
public int StrToInt(string Str, int minValue, int maxValue, int defValue)
{
try
{
int tmpI=Convert.ToInt32(Str);
if (tmpI < minValue || tmpI > maxValue)
{
return defValue;
}
else
{
return tmpI;
}
}
catch { return defValue; }
}
/// <summary>
/// 转换指定布尔类型为Int类型,true为1false为0
/// </summary>
public int BoolToInt(bool sValue)
{
try
{
if (sValue ==true)
{
return 1;
}
else
{
return 0;
}
}
catch { return 0; }
}
/// <summary>
/// 判断指定字符串是否是布尔类型
/// </summary>
public bool IsBool(string sValue)
{
try
{
Convert.ToBoolean(sValue);
return true;
}
catch { return false; }
}
/// <summary>
/// 判断指定字符串是否是Double类型
/// </summary>
public bool IsDouble(string sValue)
{
try
{
Convert.ToDouble(sValue);
return true;
}
catch { return false; }
}
/// <summary>
/// 判断指定字符串是否是Int类型
/// </summary>
public bool IsInt(string sValue)
{
try
{
Convert.ToInt64(sValue);
if (sValue.IndexOf(".")>=0)
{
return false;
}
else
{
return true;
}
}
catch { return false; }
}
/// <summary>
/// 根据年月日转换成日期
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public DateTime ToDate(int year, int month, int day)
{
try
{
return Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-" + day.ToString());
}
catch { return Convert.ToDateTime("2000-1-1"); }
}
/// <summary>
/// 追加字符
/// </summary>
public string AppendStr(string str, string addstr)
{
if (str == "")
{
return addstr;
}
else
{
if (addstr == "")
{
return str;
}
else
{
return str + "\r\n" + addstr;
}
}
}
/// <summary>
/// 获取2个字符串中间的内容,point1为空表示从首位开始算point2为空表示算到结尾。
/// </summary>
public string GetStr(string str, string point1, string point2, int iPos1,out int endPos, string defValue)
{
int iIndex = 0;
endPos = 0;
if(iPos1>= str.Length)
{
return defValue;
}
if (point1 != "")
{ iIndex = str.IndexOf(point1, iPos1); }
if (iIndex == -1) { return defValue; }
int iIndex2;
if (point2 == "") { iIndex2 = str.Length; }
else { iIndex2 = str.IndexOf(point2, iIndex + point1.Length); }
if (iIndex < iIndex2 && iIndex >= 0)
{
var tmpStr = str.Substring(iIndex + point1.Length, iIndex2 - iIndex - point1.Length);
endPos = iIndex2 + point2.Length;
return tmpStr;
}
else
{
return defValue;
}
}
/// <summary>
/// 判断字符串是否只包含数字或英文
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsEngOrNum(string str)
{
return !Regex.IsMatch(str, "[^0-9a-zA-Z]");
}
/// <summary>
/// 判断字符串是否只包含英文
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsEng(string str)
{
return !Regex.IsMatch(str, "[^a-zA-Z]");
}
/// <summary>
/// 判断字符串是否匹配,支持?*通配符
/// </summary>
/// <param name="str">待匹配的字符串</param>
/// <param name="likestr">匹配的通配符</param>
/// <returns></returns>
public static bool IsMatchStr(string str, string likestr)
{
Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]");
string a = replace.Replace(likestr,
delegate (Match m)
{
switch (m.Value)
{
case "?":
return ".?";
case "*":
return ".*";
default:
return "\\" + m.Value;
}
});
Regex strP = new Regex(a);
return strP.IsMatch(str);
//return replace.IsMatch(wildcardStr1);
}
/// <summary>
/// 通配符替换
/// </summary>
/// <param name="str"></param>
/// <param name="likestr"></param>
/// <param name="replacement"></param>
/// <returns></returns>
public static string ReplaceByMatch(string str, string likestr, string replacement)
{
Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]");
string a = replace.Replace(likestr,
delegate (Match m)
{
switch (m.Value)
{
case "?":
return ".?";
case "*":
return ".*";
default:
return "\\" + m.Value;
}
});
Regex strP = new Regex(a);
return strP.Replace(str, replacement);
//return replace.IsMatch(wildcardStr1);
}
/// <summary>
/// 支持忽略大小写的替换功能
/// </summary>
/// <param name="str"></param>
/// <param name="oldValue"></param>
/// <param name="newValue"></param>
/// <param name="IgnoreCase">是否忽略大小写</param>
/// <returns></returns>
public static string Replace(string str, string oldValue, string newValue,bool IgnoreCase)
{
if(IgnoreCase)
{
var str2 = "";
var str_tmp = str;
var pos = str_tmp.IndexOfEx(oldValue);
while(pos>=0)
{
str2 += str_tmp.Substring(0, pos)+newValue;
str_tmp = str_tmp.Substring(pos + oldValue.Length);
pos = str_tmp.IndexOfEx(oldValue);
}
str2 += str_tmp;
return str2;
}
else { return str.Replace(oldValue,newValue); }
}
/// <summary>
/// 获取匹配的内容
/// </summary>
/// <param name="str"></param>
/// <param name="likestr"></param>
/// <param name="isRegex"></param>
/// <returns></returns>
public static string GetMatchStr(string str, string likestr,bool isRegex)
{
string _likestr = likestr;
if (!isRegex)
{
Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]");
_likestr = replace.Replace(likestr,
delegate (Match m)
{
switch (m.Value)
{
case "?":
return ".?";
case "*":
return ".*";
default:
return "\\" + m.Value;
}
});
}
Match match = Regex.Match(str, _likestr);
return match.Value;
//return replace.IsMatch(wildcardStr1);
}
}
}