public string CutStr(string sInString,int iCutLength){
if(sInString==null || sInString.Length==0 || iCutLength<=0) return "";
int iCount=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
if(iCount>iCutLength){
int iLength=0;
for(int i=0;i
int iCharLength=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[]{sInString[i]});
iLength += iCharLength;
if(iLength==iCutLength){
sInString=sInString.Substring(0,i+1);
break;
}else if(iLength>iCutLength){
sInString=sInString.Substring(0,i);
break;
}
}
}
return sInString;
}
在ASP.NET或C#中,怎么截取字符串
protected string CutString(string str,int length)
{
string newString="";
if(str!="")
{
if(str.Length>length)
{
newString=str.Substring(0,length)+"...";
}
else
{
newString=str;
}
}
return newString;
}
然后在绑定的时候:
<%# CutString(DataBinder.Eval(Container.DataItem,"NewTitle").ToString(),5) %>
asp.net(c#) datelist DataGrid 中截取字符串加"..." 和 鼠标放上去字符全部显示
’ NavigateUrl=’<%# DataBinder.Eval(Container.DataItem,"url")%>’ ToolTip=’<%# DataBinder.Eval(Container.DataItem,"url")%>’>
后台.cs
protected string PartSubString(string s)
{
if(s.Length>15)
{
return s.Substring(0,15)+"...";
}
return s;
}
用C#截取指定长度的中英文混合字符串
我们常做的一件事情,就是在文章系统中,截取一定长度的文章标题,超过指定长度,就加“...”
如两个字符串:
string str1 = "中国人要啊abc呀~";
string str2 = "1中国人23456abc呀~";
要截取后,输出:
str1 = "中国人要...";
str2 = "1中国人2...";
即要把中英文混合的字符串,在截取后,长度要一致,即8个字节的长度(不包括三个点),而且不能出现中文被从中间截断的情况。于是写了个方法:
public static string getStr(string s,int l)
{
string temp = s ;
if (Regex.Replace(temp,"[/u4e00-/u9fa5]","zz",RegexOptions.IgnoreCase).Length<=l)
{
return temp;
}
for (int i=temp.Length;i>=0;i--)
{
temp = temp.Substring(0,i);
if (Regex.Replace(temp,"[/u4e00-/u9fa5]","zz",RegexOptions.IgnoreCase).Length<=l-3)
{
return temp + "";
}
}
return "";
}
调用:
string content = "中国人啊abc呀呀呀呀";
content = getStr(content,13);
string s = "iam方枪枪";
int len = s.Length;//will output as 6
byte[] sarr = System.Text.Encoding.Default.GetBytes(s);
len = sarr.Length;//will output as 3+3*2=9
{
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;
}
{
isCut=true;
break;
}
}
if(isCut)
return sb.ToString()+"..";
else
return sb.ToString();
}
C#截取指定长度中英文字符串方法 (修改)
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;
for(int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
nLength += 2;
}
else
{
nLength = nLength + 1;
}
if (nLength <= length)
{
sb.Append(stringChar[i]);
}
else
{
break;
}
}
if(sb.ToString() != stringToSub)
{
sb.Append("...");
}
return sb.ToString();
}