用c#操作字符不太熟悉,请大家帮帮忙'**************************************************
'函数名:gotTopic
'作  用:截字符串,汉字一个算两个字符,英文算一个字符
'参  数:str   ----原字符串
'       strlen ----截取长度
'返回值:截取后的字符串
'**************************************************
function gotTopic(str,strlen)
if str="" then
gotTopic=""
exit function
end if
dim l,t,c, i
str=replace(replace(replace(replace(str,"&nbsp;"," "),"&quot;",chr(34)),"&gt;",">"),"&lt;","<")
l=len(str)
t=0
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
gotTopic=left(str,i) & "…"
exit for
else
gotTopic=str
end if
next
gotTopic=replace(replace(replace(replace(gotTopic," ","&nbsp;"),chr(34),"&quot;"),">","&gt;"),"<","&lt;")
end function

解决方案 »

  1.   

    好复杂,up我喜欢这样写:s = replace (s,"&nbsp;"," ")
    s = replace (s,"&quot;",chr(34))
      

  2.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=48596EF2-8C16-47EE-1CD3-174E44FA9E24
      

  3.   

    没仔细检查:
            string gotTopic(string strSrc,int iLength)
            {
                if (strSrc==null||strSrc.Length==0) return null;            strSrc=strSrc.Replace("&nbsp;"," ");
                strSrc = strSrc.Replace("&quot;", Convert.ToString((char)34));
                strSrc=strSrc.Replace("&gt;",">");
                strSrc=strSrc.Replace("&lt;","<");            int iCurPos =0;
                int iCurLength=strSrc.Length;            for(int i=0;i<iCurLength;i++)
                {
                    if (Math.Abs(char.Parse(strSrc.Substring(i,1)))>255)
                        iCurPos+=2;
                    else
                        iCurPos+=1;                if (iCurPos>=iLength)
                    {
                        strSrc=strSrc.Substring(0,i) +"…";
                        break;
                    }
                }            strSrc = strSrc.Replace(" ", "&nbsp");
                strSrc = strSrc.Replace(Convert.ToString((char)34), "&quot;");
                strSrc = strSrc.Replace(">", "&gt;");
                strSrc = strSrc.Replace("<", "&lt;");
                return strSrc;
            }