asp.net截取字符串方法
public partial class SubString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.lblSub.Text = GetFirstString("中国人chinese", 7);
this.lblSub2.Text = GetFirstString("afafaf你好", 7);
}
public string getStr(string strInput, int intLen)
{
strInput = strInput.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
Response.Write("getStr Function is::"+myByte.Length.ToString());
if (myByte.Length > intLen)
{
//截取操作
string resultStr = "";
for (int i = 0; i < strInput.Length; i++)
{
byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
if (tempByte.Length < intLen)
{
resultStr += strInput.Substring(i, 1);
}
else
{
break;
}
}
return resultStr + "...";
}
else
{
return strInput;
}
}
public string cutString(string strInput, int intLen)
{
strInput = strInput.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
Response.Write("cutString Function is::" + myByte.Length.ToString());
if (myByte.Length > intLen)
{
//截取操作
string resultStr = "";
for (int i = 0; i < strInput.Length; i++)
{
byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
if (tempByte.Length < intLen)
{
resultStr += strInput.Substring(i, 1);
}
else
{
break;
}
}
return resultStr + "...";
}
else
{
return strInput;
}
}
public static string Fix(string s, int len)
{
string result = ""; //最终返回的结果
int byteLen = System.Text.Encoding.Default.GetByteCount(s); //单字节字符长度
int charLen = s.Length; //把字符平等对待时的字符串长度
int byteCount = 0; //记录读取进度{中文按两单位计算}
int pos = 0; //记录截取位置{中文按两单位计算}
if (byteLen > len)
{
for (int i = 0; i < charLen; i++)
{
if (Convert.ToInt32(s.ToCharArray()[i]) > 255) //遇中文字符计数加2
byteCount += 2;
else //按英文字符计算加1
byteCount += 1;
if (byteCount >= len) //到达指定长度时,记录指针位置并停止
{
pos = i;
break;
}
}
result = s.Substring(0, pos)+"...";
}
else
result = s;
return result;
}
#region 截短字串的函数,分区中英文
/// <summary>
/// 截短字串的函数
/// </summary>
/// <param name="mText">要加工的字串</param>
/// <param name="byteCount">长度</param>
/// <returns>被加工过的字串</returns>
public string Left(string mText, int byteCount)
{
if (byteCount < 1) return mText;
if (System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)
{
return mText;
}
else
{
byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
byte[] newBytes = new byte[byteCount - 4];
for (int i = 0; i < byteCount - 4; i++)
{
newBytes[i] = txtBytes[i];
}
return System.Text.Encoding.Default.GetString(newBytes) + "...";
}
}
#endregion
public static string GetFirstString(string stringToSub, int length)
{
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()))
{
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();
}
}
.Net 技术探讨群 4151320
欢迎大家加入!!!