using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//引入如下命名空间
using System.Text.RegularExpressions;
using System.Text;
/// <summary>
///StrHelper 的摘要说明
/// </summary>
public class StrHelper
{
public StrHelper(){}
/// <summary>
/// 截取字符串函数
/// </summary>
/// <param>所要截取的字符串</param>
/// <param>截取字符串的长度</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>所要截取的字符串</param>
/// <param>截取字符串的长度</param>
/// <returns></returns>
public static string GetFirstString(string stringToSub, int length)
{
#region
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))//regex.IsMatch指示正则表达式在输入字符串中是否找到匹配项
{
sb.Append(stringChar[i]);//将信息追加到当前 StringBuilder 的结尾
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
}
/// 弹出JavaScript小窗口
/// </summary>
/// <param>窗口信息</param>
public static void Alert(string message)
{
#region
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
HttpContext.Current.Response.Write(js);