[C#] 纯文本查看 复制代码 using System;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace OIFrameWork.Common
{
public class StringFunction
{
#region "方法体入口"
private static StringFunction _instance;
public static StringFunction GetInstance()
{
return _instance ?? (_instance = new StringFunction());
}
#endregion
#region "方法体"
/// <summary>
/// 返回指定的字符串在文本中出现的次数
/// </summary>
/// <param name="chars">指定要查询的字符串</param>
/// <param name="strings">需要查询的文本串</param>
/// <returns>返回出现的次数</returns>
public int GetStringCount(string chars, string strings)
{
var str = strings.Replace(chars, "");
return ( strings.Length - str.Length ) / chars.Length;
}
/// <summary>
/// 获取WebConfig中对应名称的键值信息
/// </summary>
/// <param name="appName">WebConfig应用程序名称</param>
/// <returns>返回对应的键值</returns>
public string GetConfigAppString(string appName)
{
var cacheKey = "AppSetting-" + appName;
var objModel = DataCache.GetInstance().GetCache(cacheKey);
if (objModel != null) return objModel.ToString();
objModel = ConfigurationManager.AppSettings[appName];
if ( objModel != null )
{
DataCache.GetInstance().AddCache(cacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
}
return objModel != null ? objModel.ToString() : string.Empty;
}
/// <summary>
/// 将日期字符串格式化,格式化类型为2000-00-00,2000/00/00,20000000
/// </summary>
/// <param name="dt">需要格式划的时间字符串</param>
/// <param name="dType">需要格式化的类型</param>
/// <returns>返回格式化后的时间字符串</returns>
public string GetFormatDateString(DateTime dt, DateTimeType dType)
{
if ( dType == DateTimeType.StandType )
{
return String.Format("{0}-{1}-{2}", dt.Year, dt.Month, dt.Day);
}
if ( dType == DateTimeType.ItalyType )
{
return String.Format("{0}/{1}/{2}", dt.Year, dt.Month, dt.Day);
}
if ( dType == DateTimeType.ShotType )
{
return dt.Year + dt.Month.ToString(CultureInfo.InvariantCulture) + dt.Day;
}
return string.Empty;
}
/// <summary>
/// 返回随机颜色值
/// </summary>
/// <returns>字符串表示的颜色值</returns>
public string GetRandomColorString()
{
const int length = 6;
var random = new Byte[length / 6];
var rg = new RNGCryptoServiceProvider();
rg.GetNonZeroBytes(random);
var sb = new StringBuilder(length);
int i;
for ( i = 0; i < random.Length; i++ )
{
sb.Append(String.Format("{0:X2}", random[i]));
}
return sb.ToString();
}
/// <summary>
/// 返回指定长度的随机字符串
/// </summary>
/// <param name="rLength">需要返回的长度</param>
/// <returns>返回指定长度的随机字符串</returns>
public string GetRandomString(int rLength)
{
var random = new Byte[rLength / 2];
var rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(random);
var sb = new StringBuilder(rLength);
int i;
for ( i = 0; i < random.Length; i++ )
{
sb.Append(String.Format("{0:X2}", random[i]));
}
return sb.ToString();
}
/// <summary>
/// 获取ip地址
/// </summary>
/// <returns>返回获取的Ip地址</returns>
public string GetIpAddress()
{
return HttpContext.Current.Request.UserHostAddress;
}
/// <summary>
/// 隐藏IP地址显示的最后一段地址“172.16.1.*”
/// </summary>
/// <param name="ipString">需要隐藏的IP地址</param>
/// <returns>返回隐藏后的IP地址段</returns>
public string HiddenIpAddress(string ipString)
{
return ipString.Substring(0, ipString.LastIndexOf(".", StringComparison.Ordinal)) + ".*";
}
/// <summary>
/// 获取当前的Url地址
/// </summary>
/// <returns>返回地址</returns>
public string GetUrlAddress()
{
return HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
}
/// <summary>
/// 返回文件的扩展名
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns>返回文件的扩展名</returns>
public string GetFileExpenseName(string fileName)
{
return fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1);
}
/// <summary>
/// 获取全局唯一的字符串Guid
/// </summary>
/// <returns></returns>
public string GetGuidString()
{
var guids = Guid.NewGuid().ToString().Split('-');
return guids.Aggregate(string.Empty, (current, t) => current + t.ToUpper().Trim());
}
/// <summary>
/// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
/// </summary>
/// <param name="input">需要计算的字符串</param>
/// <returns>返回字符串的长度</returns>
public int GetCount(string input)
{
return Regex.Replace(input, @"[\一-\龥/g]", "aa").Length;
}
/// <summary>
/// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
/// </summary>
/// <param name="pattern">模式字符串</param>
/// <param name="input">输入字符串</param>
/// <param name="replacement">用于替换的字符串</param>
/// <returns>返回被替换后的结果</returns>
public string Replace(string pattern, string input, string replacement)
{
var regex = new Regex(pattern);
return regex.Replace(input, replacement);
}
/// <summary>
/// 在由正则表达式模式定义的位置拆分输入字符串。
/// </summary>
/// <param name="pattern">模式字符串</param>
/// <param name="input">输入字符串</param>
/// <returns>被拆分后的字符串数组</returns>
public string[] Split(string pattern, string input)
{
var regex = new Regex(pattern);
return regex.Split(input);
}
public string SplitStringLen(string s, int len, char b)
{
if (string.IsNullOrEmpty(s))
return "";
return s.Length >= len ? s.Substring(0, len) : s.PadRight(len, b);
}
#endregion
}
}
|