public class StrHelper
{
private static string passWord; //加密字符串 /// <summary>
/// 判断输入是否数字
/// </summary>
/// <param name="num">要判断的字符串</param>
/// <returns></returns>
static public bool VldInt(string num)
{
#region
int ResultNum;
return int.TryParse(num, out ResultNum);
#endregion
} /// <summary>
/// 返回文本编辑器替换后的字符串
/// </summary>
/// <param name="str">要替换的字符串</param>
/// <returns></returns>
static public string GetHtmlEditReplace(string str)
{
#region
return str.Replace("'", "’").Replace(" ", " ").Replace(",", ",").Replace("%", "%").
Replace("script", "").Replace(".js", "");
#endregion
} /// <summary>
/// 截取字符串函数
/// </summary>
/// <param name="str">所要截取的字符串</param>
/// <param name="num">截取字符串的长度</param>
/// <returns></returns>
static public string GetSubString(string str, int num)
{
#region
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
#endregion
} /// <summary>
/// 截取字符串优化版
/// </summary>
/// <param name="stringToSub">所要截取的字符串</param>
/// <param name="length">截取字符串的长度</param>
/// <returns></returns>
public static string GetFirstString(string stringToSub, int length)
{
#region
var regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
var stringChar = stringToSub.ToCharArray();
var sb = new StringBuilder();
var nLength = 0;
var isCut = false;
for (var i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
} if (nLength > length)
{
isCut = true;
break;
}
}
if (isCut)
return sb.ToString() + "..";
else
return sb.ToString();
#endregion
} /// <summary>
/// 过滤输入信息
/// </summary>
/// <param name="text">内容</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string InputText(string text, int maxLength)
{
#region
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
#endregion
} /// <summary>
/// 生成随机数
/// </summary>
/// <returns></returns>
private string GenerateCheckCode()
{
#region
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 5; i++)
{
number = random.Next(); if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26)); checkCode += code.ToString();
} HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode;
#endregion
} /// <summary>
/// 生成验证码图片
/// </summary>
public void CreateCheckCodeImage()
{
#region
string checkCode = GenerateCheckCode();
if (checkCode == null || checkCode.Trim() == String.Empty)
return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
} var font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点
for (int i = 0; i < 150; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next()));
} //画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); var ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
#endregion
} #region 生成指定位数随机数
private static char[] constant =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
}; public static string GenerateRandom(int Length)
{
var newRandom = new StringBuilder(62);
var rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
} public static string GetNumRandom(int Length)
{
var newRandom = new StringBuilder(10);
char[] NumStr={'0','1','2','3','4','5','6','7','8','9'};
var rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(10)]);
}
return newRandom.ToString();
}
#endregion /// <summary>
/// 获取汉字第一个拼音
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static public string getSpells(string input)
{
#region
int len = input.Length;
string reVal = "";
for (int i = 0; i < len; i++)
{
reVal += getSpell(input.Substring(i, 1));
}
return reVal;
#endregion
} static public string getSpell(string cn)
{
#region
byte[] arrCN = Encoding.Default.GetBytes(cn);
if (arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area << 8) + pos;
int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int i = 0; i < 26; i++)
{
int max = 55290;
if (i != 25) max = areacode[i + 1];
if (areacode[i] <= code && code < max)
{
return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "?";
}
else return cn;
#endregion
} /// <summary>
/// 半角转全角
/// </summary>
/// <param name="BJstr"></param>
/// <returns></returns>
static public string GetQuanJiao(string BJstr)
{
#region
char[] c = BJstr.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] == 0)
{
b[0] = (byte)(b[0] - 32);
b[1] = 255;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
} string strNew = new string(c);
return strNew; #endregion
} /// <summary>
/// 全角转半角
/// </summary>
/// <param name="QJstr"></param>
/// <returns></returns>
static public string GetBanJiao(string QJstr)
{
#region
char[] c = QJstr.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];
}
}
}
string strNew = new string(c);
return strNew;
#endregion
} #region 加密的类型
/// <summary>
/// 加密的类型
/// </summary>
public enum PasswordType
{
SHA1,
MD5
}
#endregion /// <summary>
/// 字符串加密
/// </summary>
/// <param name="PasswordString">要加密的字符串</param>
/// <param name="PasswordFormat">要加密的类别</param>
/// <returns></returns>
static public string EncryptPassword(string PasswordString, PasswordType PasswordFormat)
{
#region
switch (PasswordFormat)
{
case PasswordType.SHA1:
{
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
break;
}
case PasswordType.MD5:
{
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5").Substring(8, 16).ToLower();
break;
}
default:
{
passWord = string.Empty;
break;
}
}
return passWord;
#endregion
} /// <summary>
/// 字符串转换为 html
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToHtml(string str)
{
#region
str = str.Replace("&", "&");
str = str.Replace(" ", " ");
str = str.Replace("'", "''");
str = str.Replace("\"", """);
str = str.Replace(" ", " ");
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("\r\n", "<br>"); return str;
#endregion
} /// <summary>
/// html转换成字符串
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToString(string strHtml)
{
#region
strHtml = strHtml.Replace("<br>", "\r\n");
strHtml = strHtml.Replace(@"<br />", "\r\n");
strHtml = strHtml.Replace(@"<br/>", "\r\n");
strHtml = strHtml.Replace(">", ">");
strHtml = strHtml.Replace("<", "<");
strHtml = strHtml.Replace(" ", " ");
strHtml = strHtml.Replace(""", "\""); strHtml = Regex.Replace(strHtml, @"<\/?[^>]+>", "", RegexOptions.IgnoreCase); return strHtml;
#endregion
} /// <summary>
/// 获得中文星期表示形式
/// </summary>
/// <returns></returns>
public static string GetChineseWeek(DateTime t)
{
#region
string week = ""; switch (t.DayOfWeek)
{
case DayOfWeek.Monday:
week = "一";
break;
case DayOfWeek.Tuesday:
week = "二";
break;
case DayOfWeek.Wednesday:
week = "三";
break;
case DayOfWeek.Thursday:
week = "四";
break;
case DayOfWeek.Friday:
week = "五";
break;
case DayOfWeek.Saturday:
week = "六";
break;
case DayOfWeek.Sunday:
week = "日";
break;
} return "星期" + week;
#endregion
} /// <summary>
/// 生成日期随机码
/// </summary>
/// <returns></returns>
public static string GetRamCode()
{
#region
return DateTime.Now.ToString("yyyyMMddHHmmssff");
#endregion
} /// <summary>
/// 生成指定长度的字符串,即生成strLong个str字符串
/// </summary>
/// <param name="strLong">生成的长度</param>
/// <param name="str">以str生成字符串</param>
/// <returns></returns>
public static string StringOfChar(int strLong, string str)
{
#region
string ReturnStr = string.Empty;
for (int i = 0; i < strLong; i++)
{
ReturnStr += str;
}
return ReturnStr;
#endregion
} public static string FilterStr(string str)
{ return str.Replace("'", " ").Replace(".", "").Replace("\r\n", " ");
} /// <summary>
/// 生成条形码:如:bar_code("20070520122334", 20, 1, 1);
/// </summary>
/// <param name="str"></param>
/// <param name="ch">度度</param>
/// <param name="cw">线条宽度</param>
/// <param name="type_code">是否输出文字1为输出</param>
/// <returns></returns>
public static string BarCode(object str, int ch, int cw, int type_code)
{
#region
string strTmp = str.ToString();
string code = strTmp;
strTmp = strTmp.ToLower();
int height = ch;
int width = cw; strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
strTmp = strTmp.Replace("1", "_||_|__|_|_||");
strTmp = strTmp.Replace("2", "_|_||__|_|_||");
strTmp = strTmp.Replace("3", "_||_||__|_|_|");
strTmp = strTmp.Replace("4", "_|_|__||_|_||");
strTmp = strTmp.Replace("5", "_||_|__||_|_|");
strTmp = strTmp.Replace("7", "_|_|__|_||_||");
strTmp = strTmp.Replace("6", "_|_||__||_|_|");
strTmp = strTmp.Replace("8", "_||_|__|_||_|");
strTmp = strTmp.Replace("9", "_|_||__|_||_|");
strTmp = strTmp.Replace("a", "_||_|_|__|_||");
strTmp = strTmp.Replace("b", "_|_||_|__|_||");
strTmp = strTmp.Replace("c", "_||_||_|__|_|");
strTmp = strTmp.Replace("d", "_|_|_||__|_||");
strTmp = strTmp.Replace("e", "_||_|_||__|_|");
strTmp = strTmp.Replace("f", "_|_||_||__|_|");
strTmp = strTmp.Replace("g", "_|_|_|__||_||");
strTmp = strTmp.Replace("h", "_||_|_|__||_|");
strTmp = strTmp.Replace("i", "_|_||_|__||_|");
strTmp = strTmp.Replace("j", "_|_|_||__||_|");
strTmp = strTmp.Replace("k", "_||_|_|_|__||");
strTmp = strTmp.Replace("l", "_|_||_|_|__||");
strTmp = strTmp.Replace("m", "_||_||_|_|__|");
strTmp = strTmp.Replace("n", "_|_|_||_|__||");
strTmp = strTmp.Replace("o", "_||_|_||_|__|");
strTmp = strTmp.Replace("p", "_|_||_||_|__|");
strTmp = strTmp.Replace("r", "_||_|_|_||__|");
strTmp = strTmp.Replace("q", "_|_|_|_||__||");
strTmp = strTmp.Replace("s", "_|_||_|_||__|");
strTmp = strTmp.Replace("t", "_|_|_||_||__|");
strTmp = strTmp.Replace("u", "_||__|_|_|_||");
strTmp = strTmp.Replace("v", "_|__||_|_|_||");
strTmp = strTmp.Replace("w", "_||__||_|_|_|");
strTmp = strTmp.Replace("x", "_|__|_||_|_||");
strTmp = strTmp.Replace("y", "_||__|_||_|_|");
strTmp = strTmp.Replace("z", "_|__||_||_|_|");
strTmp = strTmp.Replace("-", "_|__|_|_||_||");
strTmp = strTmp.Replace("*", "_|__|_||_||_|");
strTmp = strTmp.Replace("/", "_|__|__|_|__|");
strTmp = strTmp.Replace("%", "_|_|__|__|__|");
strTmp = strTmp.Replace("+", "_|__|_|__|__|");
strTmp = strTmp.Replace(".", "_||__|_|_||_|");
strTmp = strTmp.Replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
strTmp = strTmp.Replace("|", "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>"); if (type_code == 1)
{
return strTmp + "<BR>" + code;
}
else
{
return strTmp;
}
#endregion
} public static string ClearHtml(string HtmlString)
{
string pn = "(</?.*?/?>)";
HtmlString = Regex.Replace(HtmlString, pn, "");
return HtmlString;
} public static string ClearFormat(string HtmlString)
{
HtmlString = HtmlString.Replace("\r\n", string.Empty).Replace(" ", string.Empty);
return HtmlString.Trim();
} #region 获取N行数据
/// <summary>
/// 取指定行数数据
/// </summary>
/// <param name="str">传入的待取字符串</param>
/// <param name="rowsnum">指定的行数</param>
/// <param name="strnum">每行的英文字符数或字节数</param>
/// <returns></returns>
public static string GetContent(string str, int rowsnum, int strnum)
{
//1计算内容块
string content = str.Replace("\r\n", "§");
string[] strContent = content.Split(Convert.ToChar("§")); int strCount = rowsnum * strnum;
int cutrow = rowsnum - strContent.Length;
cutrow = rowsnum > 10 ? rowsnum : 10;
int pStrCount;
string setOkStr = ""; //2对内容块进行
for (int i = 0; i < strContent.Length; i++)
{
pStrCount = System.Text.Encoding.Default.GetBytes(strContent[i]).Length;
if (pStrCount < strCount)
{
setOkStr += strContent[i] + "<br>";
rowsnum -= Convert.ToInt32(Math.Ceiling((double)pStrCount / (double)strnum));
strCount = rowsnum * strnum;
}
else
{
if (rowsnum > 0)
{
setOkStr += CutStr(strContent[i], rowsnum * strnum, cutrow);
}
else
{
//减去rowsnum是为了避免有些行长度为90,有的为89的现像
setOkStr = setOkStr.Substring(0, setOkStr.Length - cutrow / 2) + "...";
}
break;
}
} setOkStr = setOkStr.Replace(" ", " "); //软(半角)空格转硬(全角)空格
return setOkStr; } //字符串截取函数
public static string CutStr(string str, int length, int rowsnum)
{
if (System.Text.Encoding.Default.GetBytes(str).Length < length)
return str; length = length - rowsnum;
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
i += 2;
else
i++;
if (i > length)
{
str = str.Substring(0, j) + "...";
break;
}
j++;
}
return str; }
#endregion #region 得到整型
public static int ConvertToInt(string Str)
{
return Str.Trim() == string.Empty ? 0 : int.Parse(Str);
} public static int GetInt(object o)
{
#region
if (o == DBNull.Value || o == null)
return 0;
else
return Convert.ToInt32(o);
#endregion
}
#endregion /// <summary>
/// 转化成时间类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static object ConvertDate(object o)
{
DateTime dt;
if(DateTime.TryParse(o.ToString(),out dt))
return dt;
else
return DBNull.Value;
} #region 闭合HTML代码
public static string CloseHTML(string str)
{
string[] HtmlTag = new string[] { "p", "div", "span", "table", "ul", "font", "b", "u", "i", "a", "h1", "h2", "h3", "h4", "h5", "h6" }; for (int i = 0; i < HtmlTag.Length; i++)
{
int OpenNum = 0, CloseNum = 0;
Regex re = new Regex("<" + HtmlTag + "[^>]*" + ">", RegexOptions.IgnoreCase);
MatchCollection m = re.Matches(str);
OpenNum = m.Count;
re = new Regex("</" + HtmlTag + ">", RegexOptions.IgnoreCase);
m = re.Matches(str);
CloseNum = m.Count; for (int j = 0; j < OpenNum - CloseNum; j++)
{
str += "</" + HtmlTag + ">";
}
} return str;
}
#endregion /// <summary>
/// 得到192.248.23.*的IP
/// </summary>
/// <param name="Str">IP地址</param>
/// <returns></returns>
public static string GetSortIp(string Str)
{
int x = Str.LastIndexOf('.')-1;
return Str.Substring(0, x) + "*.*";
} /// <summary>
/// 获取年月
/// </summary>
/// <returns></returns>
public static string GetYearMonth()
{
return DateTime.Now.ToString("yyyyMM") ;
} #region 获取远程页面内容
public static string GetHttpData(string Url)
{
//string sException = null;
string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
//sException = e.Message.ToString();
//Response.Write(sException);
}
catch (Exception e)
{
//sException = e.ToString();
//Response.Write(sException);
}
finally
{
if (oWebRps != null)
{
var oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("UTF-8"));//GB2312|UTF-8"
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
} public string[] GetData(string Html)
{
String[] rS = new String[2];
string s = Html;
s = Regex.Replace(s, "\\s{3,}", "");
s = s.Replace("\r", "");
s = s.Replace("\n", "");
string Pat = "<td align=\"center\" class=\"24p\"><B>(.*)</B></td></tr><tr>.*(<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">.*</table>)<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">(.*)<td align=center class=l6h>";
Regex Re = new Regex(Pat);
Match Ma = Re.Match(s);
if (Ma.Success)
{
rS[0] = Ma.Groups[1].ToString();
rS[1] = Ma.Groups[2].ToString();
//pgStr = Ma.Groups[3].ToString();
}
return rS;
}
#endregion /// <summary>
/// 判断页面是否存在
/// </summary>
/// <param name="sURL"></param>
/// <returns></returns>
public static bool UrlExist(string sURL)
{
#region
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
//WebProxy proxy = new WebProxy("your proxy server", 8080);
//request.Proxy = proxy;
request.Method = "HEAD";
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
bool result = false;
switch (response.StatusCode)
{
case HttpStatusCode.OK:
result = true;
break;
case HttpStatusCode.Moved:
break;
case HttpStatusCode.NotFound:
break;
}
response.Close();
return result;
}
catch
{
return false;
}
#endregion
} #region 获取字串中的链接
/// <summary>
/// 获取字串中的链接
/// </summary>
/// <param name="HtmlCode"></param>
/// <returns></returns>
public static ArrayList GetPageUrl(string HtmlCode)
{
ArrayList my_list = new ArrayList();
string p = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
Regex re = new Regex(p, RegexOptions.IgnoreCase);
MatchCollection mc = re.Matches(HtmlCode); for (int i = 0; i <= mc.Count - 1; i++)
{
string name = mc[i].ToString();
if (!my_list.Contains(name))//排除重复URL
{
my_list.Add(name);
}
}
return my_list;
}
#endregion /// <summary>
/// 将 Stream 转化成 string
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ConvertStreamToString(Stream s)
{
#region
string strResult = "";
StreamReader sr = new StreamReader(s, Encoding.UTF8); Char[] read = new Char[256]; // Read 256 charcters at a time.
int count = sr.Read(read, 0, 256); while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
string str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
} // 释放资源
sr.Close(); return strResult;
#endregion
} /// <summary>
/// 对传递的参数字符串进行处理,防止注入式攻击
/// </summary>
/// <param name="str">传递的参数字符串</param>
/// <returns>String</returns>
public static string ConvertSql(string str)
{
#region
str = str.Trim();
str = str.Replace("'", "''");
str = str.Replace(";--", "");
str = str.Replace("=", "");
str = str.Replace(" or ", "");
str = str.Replace(" and ", ""); return str;
#endregion
} /// <summary>
/// 格式化占用空间大小的输出
/// </summary>
/// <param name="size">大小</param>
/// <returns>返回 String</returns>
public static string FormatNUM(long size)
{
#region
decimal NUM;
string strResult; if (size > 1073741824)
{
NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1073741824));
strResult = NUM.ToString("N") + " M";
}
else if (size > 1048576)
{
NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1048576));
strResult = NUM.ToString("N") + " M";
}
else if (size > 1024)
{
NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1024));
strResult = NUM.ToString("N") + " KB";
}
else
{
strResult = size + " 字节";
} return strResult;
#endregion
} /// <summary>
/// 获取数组元素的合并字符串
/// </summary>
/// <param name="stringArray"></param>
/// <returns></returns>
public static string GetArrayString(string[] stringArray)
{
#region
string totalString = null;
for (int i = 0; i < stringArray.Length; i++)
{
totalString = totalString + stringArray[i];
}
return totalString;
#endregion
} /// <summary>
/// 将指定字符串中的汉字转换为拼音首字母的缩写,其中非汉字保留为原字符
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string ConvertSpellFirst(string text)
{
#region var sb = new StringBuilder(text.Length);
foreach (char c in text)
{
var pinyin = c;
byte[] array = Encoding.Default.GetBytes(new[] { c }); if (array.Length == 2)
{
var i = array[0] * 0x100 + array[1]; #region 条件匹配
if (i < 0xB0A1) pinyin = c;
else
if (i < 0xB0C5) pinyin = 'a';
else
if (i < 0xB2C1) pinyin = 'b';
else
if (i < 0xB4EE) pinyin = 'c';
else
if (i < 0xB6EA) pinyin = 'd';
else
if (i < 0xB7A2) pinyin = 'e';
else
if (i < 0xB8C1) pinyin = 'f';
else
if (i < 0xB9FE) pinyin = 'g';
else
if (i < 0xBBF7) pinyin = 'h';
else
if (i < 0xBFA6) pinyin = 'g';
else
if (i < 0xC0AC) pinyin = 'k';
else
if (i < 0xC2E8) pinyin = 'l';
else
if (i < 0xC4C3) pinyin = 'm';
else
if (i < 0xC5B6) pinyin = 'n';
else
if (i < 0xC5BE) pinyin = 'o';
else
if (i < 0xC6DA) pinyin = 'p';
else
if (i < 0xC8BB) pinyin = 'q';
else
if (i < 0xC8F6) pinyin = 'r';
else
if (i < 0xCBFA) pinyin = 's';
else
if (i < 0xCDDA) pinyin = 't';
else
if (i < 0xCEF4) pinyin = 'w';
else
if (i < 0xD1B9) pinyin = 'x';
else
if (i < 0xD4D1) pinyin = 'y';
else
if (i < 0xD7FA) pinyin = 'z';
#endregion
} sb.Append(pinyin);
} return sb.ToString();
#endregion
} #region Request
/// <summary></summary>
/// <param name="filedName"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int Request(string filedName, int defaultValue)
{
return Request(filedName, defaultValue, false);
} /// <summary>
/// 获取URL指定参数,如果参数不存在或者为空就返回默认值
/// </summary>
/// <param name="filedName">请求KEY</param>
/// <param name="defaultValue">默认值</param>
/// <param name="isDecrypt">是否需要解密</param>
/// <returns></returns>
public static int Request(string filedName, int defaultValue, bool isDecrypt)
{
string str = HttpContext.Current.Request[filedName];
if (str != null && str.Trim() != "")
{
if (isDecrypt)
{
str = new PblogDes().Decrypt(str);
}
return int.Parse(str);
} return defaultValue;
} /// <summary>
/// 获取URL指定参数,如果参数不存在或者为空就抛出错误信息
/// </summary>
/// <param name="filedName"></param>
/// <returns></returns>
public static int Request(string filedName)
{
return Request(filedName, false);
} /// <summary>
/// 获取URL指定参数,如果参数不存在或者为空就抛出错误信息
/// </summary>
/// <param name="filedName"></param>
/// <param name="isDecrypt">是否需要解密</param>
/// <returns></returns>
public static int Request(string filedName, bool isDecrypt)
{
int i = Request(filedName, -1, isDecrypt);
if (i == -1)
{
throw new Exception("找不到指定参数:" + filedName);
} return i;
}
#endregion
}

StrHelper的更多相关文章

  1. C#基础系列——委托实现简单设计模式

    前言:上一篇介绍了下多线程的相关知识:C#基础系列——多线程的常见用法详解,里面就提到了委托变量.这篇简单介绍下委托的使用.当然啦,园子里面很多介绍委托的文章都会说道:委托和事件的概念就像一道坎,过了 ...

  2. discuz论坛与其它网站登录注册整合

    discuz论坛与其它网站登录注册整合 本文以discuz 7.0.0 php版本的论坛与 .net 2.0的网站注册登录整合为类.没有采用uc_center或第三方插件.以另类的方式实现.此方法实现 ...

  3. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  4. 024. asp.net中第一次使用GridView (设置鼠标经过时更换背景色)

    1. 前端HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inde ...

  5. lucene 根据 id 获取索引内容(C# / .NET)

    封装代码如下: public MakerArtiImodel GetIndexModelById(int id) { string indexPath = StrHelper.rootPath + & ...

  6. 生成图片验证码(.NET)

    一.生成随机字符串 方法一: public string CreateRandomCode(int codeCount) { string allChar = "0,1,2,3,4,5,6, ...

  7. 创建可复用的自定义 ASP.NET MVC Helpers

    通常,在ASP.NET MVC项目中App_Code目录下新建.cshtml编写类似下方的代码就能创建自定义的MVC Helper了, 假设文件名为StrHelper.cshtml,那么在别的视图中的 ...

  8. c# mysql blob数据类型

    1.采用stream流形式写入: #region 数据流转换成blob类型数据写入数据库 static public bool StreamToBlob(ref Stream stream, Odbc ...

  9. 数据结构( Pyhon 语言描述 ) — —第10章:树

    树的概览 树是层级式的集合 树中最顶端的节点叫做根 个或多个后继(子节点). 没有子节点的节点叫做叶子节点 拥有子节点的节点叫做内部节点 ,其子节点位于层级1,依次类推.一个空树的层级为 -1 树的术 ...

随机推荐

  1. 笔记-iOS弹幕(源码)实现原理解析

    最近,读完今年的第三本书<大话移动APP测试 Android与iOS>,在读到陈晔前辈改变中国测试行业的决心时,内心无比激动,作为一名初生的开发人员,我可能还无法理解测试行业的本质,但他那 ...

  2. oscgit

    Gitlab PaaS项目演示 git config --global user.name "你的名字或昵称" git config --global user.email &qu ...

  3. Ajax调用WebService(一)

    Ajax调用WebService(一) http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html 分类: Ajax 使用技术 We ...

  4. Spring Framework 5.0.0.M3中文文档 翻译记录 Part I. Spring框架概览2.3

    2.3 Usage scenarios 使用场景 The building blocks described previously make Spring a logical choice in ma ...

  5. Mysql 培训

     1. Mysql 培训 1.1. 培训目的 本文档是针对MySQL 数据库方面的基础培训,为了使项目组成员能够达到使用MySQL 数据库的目的. 1.2. 培训对象 开发者 1.3. 经常使用词及符 ...

  6. yarn的基本组成和工作流程

    yarn是负责资源管理的,协调各个应用程序的资源使用情况 一.基本组成 yarn主要由以下几个部分组成 1.resourcemanager 主要负责资源的调度和应用程序的管理 (1)调度器 调度器是将 ...

  7. jbpm入门样例

    1.      jBPM的简介 jBPM是JBOSS下的一个开源java工作流项目,该项目提供eclipse插件,基于Hibernate实现数据持久化存储.    參考 http://www.jbos ...

  8. AIR 程序开发系列 之五 保存数据的几种方式

    Local SharedObject 这种方法比较简单方便的保存少的数据到到设备中.你不用自己去管理这些数据,设备会自动管理他. SharedObject 在 flash.net 包中,继承自Even ...

  9. VC 部署

    部署是分发要安装到其他计算机上的已完成应用程序或组件的过程. 当使用 Visual C++ 库提供的功能生成 C/C++ 应用程序时,该应用程序在运行时将依赖于那些库的存在. 为了让应用程序运行,它必 ...

  10. careercup-C和C++ 13.4

    13.4 深拷贝和浅拷贝有什么区别,如何使用? 解答 浅拷贝并不复制数据,只复制指向数据的指针,因此是多个指针指向同一份数据. 深拷贝会复制原始数据,每个指针指向一份独立的数据.通过下面的代码, 可以 ...