http://hi.baidu.com/netshahuhu/blog/item/2b33cd94e89f3b1ad21b700a.html/cmtid/c5ff70876d0dae22c65cc317

用C#写的字符串截取函数(转)

2008年01月18日 10:45用C#写的字符串截取函数

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<sInString.Length;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 中截取字符串加"..." 和 鼠标放上去字符全部显示
前台
<asp:TemplateColumn HeaderText="绑定网址">
<HeaderStyle HorizontalAlign="Center" Width="100px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<FONT face="宋体">
<asp:HyperLink );
}
return sb.ToString();
}

相关文章: