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) %>

 截取中英文混合字符串

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 = "同样会突然认同和法国回复回复规划";
content = getStr(content,13);

posted @ 2008-09-01 20:43  郑希强  阅读(262)  评论(0编辑  收藏  举报