微博内容长度的计算方法(不一定完全无误,仅提供一个思路):

       public static int GetWeiboContentLength(string weiboContent)
       {
           var max = 140;
           var surl = 11;
           var urlCount = 0;

           RegexOptions ops = RegexOptions.Compiled;
           Regex regex = new Regex(@"http://[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+([-A-Z0-9a-z_\$\.\+\!\*\(\)\/,:;@&=\?\~\#\%]*)", ops);
           var content = weiboContent;
           if (regex.IsMatch(weiboContent))
           {
               var matchs = regex.Matches(weiboContent);
               for (int i = 0; i < matchs.Count; i++)
               {
                   var count = matchs[i].Groups[0].Length;
                   urlCount += count <= max ? surl : (count - max + surl);
                   content = content.Replace(matchs[i].Value, "");
               }
           }
           var result = urlCount + content.Length;
           return result;
       }

相关文章:

  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-11-16
  • 2021-07-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2022-03-06
  • 2021-11-29
相关资源
相似解决方案