using System;
using System.Text;
using System.Text.RegularExpressions;
namespace bjmti
{
/// <summary>
/// include 的摘要说明。
/// </summary>
public class include
{
public include()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 字符串截取函数
/// <summary>
/// 字符串截取函数
/// </summary>
/// <param name="inputString">要截取的字符串</param>
/// <param name="len">要截取的长度</param>
/// <returns>string</returns>
///
public static string CutString(string inputString,int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen=0;
string tempString="";
byte[] s = ascii.GetBytes(inputString);
for(int i=0;i<s.Length;i++)
{
if((int)s[i]==63)
{
tempLen+=2;
}
else
{
tempLen+=1;
}
try
{
tempString+=inputString.Substring(i,1);
}
catch
{
break;
}
if(tempLen>len)
break;
}
// //如果截过则加上半个省略号
// byte[] mybyte=System.Text.Encoding.Default.GetBytes(inputString);
// if(mybyte.Length>len)
// tempString+="…";
return tempString;
}
#endregion
#region 生成由日期组成的唯一的文件名
/// <summary>
/// 生成由日期组成的唯一的文件名
/// </summary>
/// <returns>string</returns>
///
public static string makeFileName()
{
string newFileName;
string dateName = System.DateTime.Now.ToString("yyyyMMddhhmmss");
System.Random srd = new Random();
int srdName = srd.Next(1000);
newFileName = dateName+srdName.ToString();
return newFileName;
}
#endregion
#region 过滤特殊字符
/// <summary>
/// 过滤特殊字符
/// </summary>
/// <param name="inputStr">字符串</param>
/// <returns>string</returns>
public static string cutBadStr(string inputStr)
{
inputStr = inputStr.ToLower().Replace(",","");
inputStr = inputStr.ToLower().Replace("<","<");
inputStr = inputStr.ToLower().Replace(">",">");
inputStr = inputStr.ToLower().Replace("%","");
inputStr = inputStr.ToLower().Replace(".","");
inputStr = inputStr.ToLower().Replace(":","");
inputStr = inputStr.ToLower().Replace("#","");
inputStr = inputStr.ToLower().Replace("&","");
inputStr = inputStr.ToLower().Replace("$","");
inputStr = inputStr.ToLower().Replace("^","");
inputStr = inputStr.ToLower().Replace("*","");
inputStr = inputStr.ToLower().Replace("`","");
inputStr = inputStr.ToLower().Replace(" ","");
inputStr = inputStr.ToLower().Replace("~","");
inputStr = inputStr.ToLower().Replace("or","");
inputStr = inputStr.ToLower().Replace("and","");
return inputStr;
}
#endregion
#region 过滤html标记
/// <summary>
/// 过滤html标记
/// </summary>
/// <param name="HTMLStr">要过滤的字符串</param>
/// <returns>string</returns>
///
public static string CutHTML(string strHtml)
{
string [] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(http://www.cnblogs.com/lds85930/admin/file://[%22%22'tbnr]%7c[%5e/7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
};
string [] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\n",
""
};
string newReg =aryReg[0];
string strOutput=strHtml;
for(int i = 0;i<aryReg.Length;i++)
{
Regex regex = new Regex(aryReg[i],RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput,aryRep[i]);
}
strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("\r\n","");
return strOutput;
}
#endregion
#region 标题固定长度
/// <summary>
/// <table style="font-size:12px">
/// <tr><td><b>功能描述</b>:填充或截断原始字符串为指定长度 </td></tr>
/// <tr><td><b>创 建 人</b>: </td></tr>
/// <tr><td><b>创建时间</b>:</td></tr>
/// </table>
/// </summary>
/// <param name="strOriginal">原始字符串</param>
/// <param name="maxTrueLength">字符串的字节长度</param>
/// <param name="chrPad">填充字符</param>
/// <param name="blnCutTail">字符串的字节长度超过maxTrueLength时截断多余字符</param>
/// <returns>填充或截断后的字符串</returns>
static public string PadRightTrueLen(string strOriginal,int maxTrueLength,char chrPad,bool blnCutTail)
{
string strNew = strOriginal;
if (strOriginal==null || maxTrueLength<=0)
{
strNew = "";
return strNew;
}
int trueLen = TrueLength(strOriginal);
if (trueLen>maxTrueLength)//超过maxTrueLength
{
if (blnCutTail)//截断
{
for(int i=strOriginal.Length-1; i>0; i--)
{
strNew = strNew.Substring(0,i);
if (TrueLength(strNew) == maxTrueLength)
break;
else if (TrueLength(strNew) < maxTrueLength)
{
strNew += chrPad.ToString();
break;
}
}
}
}
else//填充
{
for(int i=0;i<maxTrueLength-trueLen;i++)
{
strNew += chrPad.ToString();
}
}
return strNew;
}
//主方法
public static string CutStringTitle(string inputString,int i)
{
return PadRightTrueLen(inputString,i,' ',true);
}
/// <summary>
/// <table style="font-size:12px">
/// <tr><td><b>功能描述</b>:字符串的字节长度 </td></tr>
/// <tr><td><b>创 建 人</b>: </td></tr>
/// <tr><td><b>创建时间</b>:</td></tr>
/// </table>
/// </summary>
/// <param name="str">字符串</param>
/// <returns>字符串的字节长度</returns>
static public int TrueLength(string str)
{
int lenTotal = 0;
int n = str.Length;
string strWord = "";
int asc;
for(int i=0;i<n;i++)
{
strWord = str.Substring(i,1);
asc = Convert.ToChar(strWord);
if ( asc < 0 || asc > 127 )
lenTotal = lenTotal + 2;
else
lenTotal = lenTotal + 1;
}
return lenTotal;
}
#endregion
#region 关闭当前窗口(静态)
/// <summary>
/// (Descript) 关闭当前窗口(静态)
/// (Author) warfu
/// (Date) 2007-08-28
/// </summary>
public static void CloseWindow()
{
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=JAVASCRIPT>window.close()</SCRIPT>");
}
#endregion
#region 打开没有Toolbar的新窗口(静态)
/// <summary>
/// 返回js脚本——window.open('','')
/// </summary>
/// <param name="url">网址</param>
/// <param name="name">目标</param>
/// <returns></returns>
public static string openWeb(string url,string name)
{
//输出提示信息
return "<script language=javascript>window.open('"+ url +"','"+ name +"')</script>";
}
/// <summary>
/// 打开没有Toolbar的新窗口(静态)
/// </summary>
/// <param name="str_Url">页面路径</param>
/// <param name="int_Height">窗口高度</param>
/// <param name="int_Width">窗口宽度</param>
/// <param name="str_WebName">窗口名称</param>
public static void OpenWindow(string str_Url,int int_Height,int int_Width,string str_WebName)
{
//构造JAVASCRIPT
string str_JavaScript = @"<Script language='JavaScript'>
window.open('"mailto:+str_Url+@%22','%22+str_WebName+@%22','height="+ int_Height mailto:+@%22','width="+ int_Width mailto:+@%22','top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,directories=no');
</Script>";
System.Web.HttpContext.Current.Response.Write(str_JavaScript);
}
#endregion
}
}
我们知道,在C#(.Net)内部所使用Unicode来存储字符串,这样在处理字符串的时候偶尔会发生一些不爽的事情。比如,如果字符串中有中文字符,而我们想取得固定长度字符串就颇为麻烦,下面提供了一个解决方案。在这个方案中,我们实现了这么几个功能:
取得给出字符串靠左边的子串,子串的大小为为指定长度;
如果给出的字符串长度大于给出的长度,则在子串的末端加上“..”。
避免出现半个汉字的情况。
源码如下:
private string getLeftSubString(string content, int length)
{
Encoding encoding = Encoding.GetEncoding("gb2312");
StringBuilder sb = new StringBuilder();
int totalLength = 0;
foreach (char contentChar in content)
{
int size = encoding.GetByteCount(new char[] { contentChar });
if (totalLength + size > length -2)
{
sb.Append("..");
break;
}
sb.Append(contentChar);
totalLength += size;
}
return sb.ToString();
}